blob: d143a888fd0972c5fd3c33e2732f88733e6e5772 [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() {
Roman Stratiienko4f1effa2021-09-29 12:47:26 +030040 uint64_t enum_value = UINT64_MAX;
Sean Paul6a55e9f2015-04-30 15:31:06 -040041 DrmProperty p;
42
43 int ret = drm_->GetPlaneProperty(*this, "type", &p);
44 if (ret) {
45 ALOGE("Could not get plane type property");
46 return ret;
47 }
48
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020049 uint64_t type = 0;
Sean Paulfc0b1da2019-03-06 09:48:42 -050050 std::tie(ret, type) = p.value();
Sean Paul6a55e9f2015-04-30 15:31:06 -040051 if (ret) {
52 ALOGE("Failed to get plane type property value");
53 return ret;
54 }
55 switch (type) {
56 case DRM_PLANE_TYPE_OVERLAY:
57 case DRM_PLANE_TYPE_PRIMARY:
58 case DRM_PLANE_TYPE_CURSOR:
59 type_ = (uint32_t)type;
60 break;
61 default:
Sean Paulf741c672016-05-11 13:49:38 -040062 ALOGE("Invalid plane type %" PRIu64, type);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 return -EINVAL;
64 }
65
66 ret = drm_->GetPlaneProperty(*this, "CRTC_ID", &crtc_property_);
67 if (ret) {
68 ALOGE("Could not get CRTC_ID property");
69 return ret;
70 }
71
72 ret = drm_->GetPlaneProperty(*this, "FB_ID", &fb_property_);
73 if (ret) {
74 ALOGE("Could not get FB_ID property");
75 return ret;
76 }
77
78 ret = drm_->GetPlaneProperty(*this, "CRTC_X", &crtc_x_property_);
79 if (ret) {
80 ALOGE("Could not get CRTC_X property");
81 return ret;
82 }
83
84 ret = drm_->GetPlaneProperty(*this, "CRTC_Y", &crtc_y_property_);
85 if (ret) {
86 ALOGE("Could not get CRTC_Y property");
87 return ret;
88 }
89
90 ret = drm_->GetPlaneProperty(*this, "CRTC_W", &crtc_w_property_);
91 if (ret) {
92 ALOGE("Could not get CRTC_W property");
93 return ret;
94 }
95
96 ret = drm_->GetPlaneProperty(*this, "CRTC_H", &crtc_h_property_);
97 if (ret) {
98 ALOGE("Could not get CRTC_H property");
99 return ret;
100 }
101
102 ret = drm_->GetPlaneProperty(*this, "SRC_X", &src_x_property_);
103 if (ret) {
104 ALOGE("Could not get SRC_X property");
105 return ret;
106 }
107
108 ret = drm_->GetPlaneProperty(*this, "SRC_Y", &src_y_property_);
109 if (ret) {
110 ALOGE("Could not get SRC_Y property");
111 return ret;
112 }
113
114 ret = drm_->GetPlaneProperty(*this, "SRC_W", &src_w_property_);
115 if (ret) {
116 ALOGE("Could not get SRC_W property");
117 return ret;
118 }
119
120 ret = drm_->GetPlaneProperty(*this, "SRC_H", &src_h_property_);
121 if (ret) {
122 ALOGE("Could not get SRC_H property");
123 return ret;
124 }
125
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100126 ret = drm_->GetPlaneProperty(*this, "zpos", &zpos_property_);
127 if (ret)
128 ALOGE("Could not get zpos property for plane %u", id());
129
Sean Paul1c4c3262015-07-14 15:51:52 -0400130 ret = drm_->GetPlaneProperty(*this, "rotation", &rotation_property_);
131 if (ret)
132 ALOGE("Could not get rotation property");
133
Sean Pauld8aefb62015-10-15 15:17:31 -0400134 ret = drm_->GetPlaneProperty(*this, "alpha", &alpha_property_);
135 if (ret)
136 ALOGI("Could not get alpha property");
137
Lowry Li9b6cafd2018-08-28 17:58:21 +0800138 ret = drm_->GetPlaneProperty(*this, "pixel blend mode", &blend_property_);
Roman Stratiienko5063d532021-09-29 12:47:31 +0300139 if (ret == 0) {
140 std::tie(enum_value,
141 ret) = blend_property_.GetEnumValueWithName("Pre-multiplied");
142 if (ret == 0) {
143 blending_enum_map_[DrmHwcBlending::kPreMult] = enum_value;
144 }
145 std::tie(enum_value,
146 ret) = blend_property_.GetEnumValueWithName("Coverage");
147 if (ret == 0) {
148 blending_enum_map_[DrmHwcBlending::kCoverage] = enum_value;
149 }
150 std::tie(enum_value, ret) = blend_property_.GetEnumValueWithName("None");
151 if (ret == 0) {
152 blending_enum_map_[DrmHwcBlending::kNone] = enum_value;
153 }
154 } else {
Lowry Li9b6cafd2018-08-28 17:58:21 +0800155 ALOGI("Could not get pixel blend mode property");
Roman Stratiienko5063d532021-09-29 12:47:31 +0300156 }
Lowry Li9b6cafd2018-08-28 17:58:21 +0800157
Robert Fossa09220c2016-09-30 10:27:23 -0400158 ret = drm_->GetPlaneProperty(*this, "IN_FENCE_FD", &in_fence_fd_property_);
159 if (ret)
160 ALOGI("Could not get IN_FENCE_FD property");
161
Matvii Zorin8338c342020-09-08 16:12:51 +0300162 if (HasNonRgbFormat()) {
163 ret = drm_->GetPlaneProperty(*this, "COLOR_ENCODING",
164 &color_encoding_propery_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300165 if (ret == 0) {
166 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
167 "ITU-R BT.709 YCbCr");
168 if (ret == 0) {
169 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec709] = enum_value;
170 }
171 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
172 "ITU-R BT.601 YCbCr");
173 if (ret == 0) {
174 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec601] = enum_value;
175 }
176 std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
177 "ITU-R BT.2020 YCbCr");
178 if (ret == 0) {
179 color_encoding_enum_map_[DrmHwcColorSpace::kItuRec2020] = enum_value;
180 }
181 } else {
Matvii Zorin8338c342020-09-08 16:12:51 +0300182 ALOGI("Could not get COLOR_ENCODING property");
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300183 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300184
185 ret = drm_->GetPlaneProperty(*this, "COLOR_RANGE", &color_range_property_);
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300186 if (ret == 0) {
187 std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
188 "YCbCr full range");
189 if (ret == 0) {
190 color_range_enum_map_[DrmHwcSampleRange::kFullRange] = enum_value;
191 }
192 std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
193 "YCbCr limited range");
194 if (ret == 0) {
195 color_range_enum_map_[DrmHwcSampleRange::kLimitedRange] = enum_value;
196 }
197 } else {
Matvii Zorin8338c342020-09-08 16:12:51 +0300198 ALOGI("Could not get COLOR_RANGE property");
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300199 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300200 }
201
Sean Paul6a55e9f2015-04-30 15:31:06 -0400202 return 0;
203}
204
205uint32_t DrmPlane::id() const {
206 return id_;
207}
208
209bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200210 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400211}
212
Matvii Zorin67a89d32021-01-31 14:45:05 +0200213bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
Benjamin Lib00b28d2021-05-09 08:52:18 -0700214 if (rotation_property_.id() == 0) {
215 if (layer->transform != DrmHwcTransform::kIdentity) {
216 ALOGV("Rotation is not supported on plane %d", id_);
217 return false;
218 }
219 } else {
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
223 // not the former).
224 int ret = 0;
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"}};
231
232 for (const auto &[transform, name] : transforms) {
233 if (layer->transform & transform) {
234 std::tie(std::ignore,
235 ret) = rotation_property_.GetEnumValueWithName(name);
236 if (ret) {
237 ALOGV("Rotation '%s' is not supported on plane %d", name.c_str(),
238 id_);
239 return false;
240 }
241 }
242 }
Matvii Zorin67a89d32021-01-31 14:45:05 +0200243 }
244
245 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
246 ALOGV("Alpha is not supported on plane %d", id_);
247 return false;
248 }
249
Roman Stratiienko5063d532021-09-29 12:47:31 +0300250 if (blending_enum_map_.count(layer->blending) == 0 &&
251 layer->blending != DrmHwcBlending::kNone) {
252 ALOGV("Blending is not supported on plane %d", id_);
253 return false;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200254 }
255
Roman Stratiienko51287152021-02-10 14:59:52 +0200256 uint32_t format = layer->buffer_info.format;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200257 if (!IsFormatSupported(format)) {
258 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
259 format >> 8, format >> 16, format >> 24);
260 return false;
261 }
262
263 return true;
264}
265
Sean Paul6a55e9f2015-04-30 15:31:06 -0400266uint32_t DrmPlane::type() const {
267 return type_;
268}
269
Roman Kovalivskyi859b6072020-03-26 05:03:39 +0200270bool DrmPlane::IsFormatSupported(uint32_t format) const {
271 return std::find(std::begin(formats_), std::end(formats_), format) !=
272 std::end(formats_);
273}
274
Matvii Zorin8338c342020-09-08 16:12:51 +0300275bool 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_);
280}
281
Roman Stratiienko6662f712021-09-29 12:52:22 +0300282static uint64_t ToDrmRotation(DrmHwcTransform transform) {
283 uint64_t rotation = 0;
284 if (transform & DrmHwcTransform::kFlipH)
285 rotation |= DRM_MODE_REFLECT_X;
286 if (transform & DrmHwcTransform::kFlipV)
287 rotation |= DRM_MODE_REFLECT_Y;
288 if (transform & DrmHwcTransform::kRotate90)
289 rotation |= DRM_MODE_ROTATE_90;
290 else if (transform & DrmHwcTransform::kRotate180)
291 rotation |= DRM_MODE_ROTATE_180;
292 else if (transform & DrmHwcTransform::kRotate270)
293 rotation |= DRM_MODE_ROTATE_270;
294 else
295 rotation |= DRM_MODE_ROTATE_0;
296
297 return rotation;
298}
299
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300300auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
301 uint32_t zpos, uint32_t crtc_id) -> int {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300302 if (!layer.FbIdHandle) {
303 ALOGE("Expected a valid framebuffer for pset");
304 return -EINVAL;
305 }
306
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300307 if (zpos_property_ && !zpos_property_.is_immutable()) {
308 uint64_t min_zpos = 0;
309
310 // Ignore ret and use min_zpos as 0 by default
311 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
312
313 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
314 return -EINVAL;
315 }
316 }
317
Roman Stratiienko6662f712021-09-29 12:52:22 +0300318 if (layer.acquire_fence &&
319 !in_fence_fd_property_.AtomicSet(pset, layer.acquire_fence.Get())) {
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300320 return -EINVAL;
321 }
322
Roman Stratiienko6662f712021-09-29 12:52:22 +0300323 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
324 !fb_property_.AtomicSet(pset, layer.FbIdHandle->GetFbId()) ||
325 !crtc_x_property_.AtomicSet(pset, layer.display_frame.left) ||
326 !crtc_y_property_.AtomicSet(pset, layer.display_frame.top) ||
327 !crtc_w_property_.AtomicSet(pset, layer.display_frame.right -
328 layer.display_frame.left) ||
329 !crtc_h_property_.AtomicSet(pset, layer.display_frame.bottom -
330 layer.display_frame.top) ||
331 !src_x_property_.AtomicSet(pset, (int)(layer.source_crop.left) << 16) ||
332 !src_y_property_.AtomicSet(pset, (int)(layer.source_crop.top) << 16) ||
333 !src_w_property_.AtomicSet(pset, (int)(layer.source_crop.right -
334 layer.source_crop.left)
335 << 16) ||
336 !src_h_property_.AtomicSet(pset, (int)(layer.source_crop.bottom -
337 layer.source_crop.top)
338 << 16)) {
339 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300340 }
341
Roman Stratiienko6662f712021-09-29 12:52:22 +0300342 if (rotation_property_ &&
343 !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.transform))) {
344 return -EINVAL;
345 }
346
347 if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.alpha)) {
348 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300349 }
350
Roman Stratiienko5063d532021-09-29 12:47:31 +0300351 if (blending_enum_map_.count(layer.blending) != 0 &&
352 !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
353 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300354 }
355
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300356 if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
357 !color_encoding_propery_
358 .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
359 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300360 }
361
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300362 if (color_range_enum_map_.count(layer.sample_range) != 0 &&
363 !color_range_property_
364 .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
365 return -EINVAL;
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300366 }
Roman Stratiienko4f1effa2021-09-29 12:47:26 +0300367
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300368 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400369}
370
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300371auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
372 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
373 return -EINVAL;
374 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400375
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300376 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400377}
Sean Paul1c4c3262015-07-14 15:51:52 -0400378
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100379const DrmProperty &DrmPlane::zpos_property() const {
380 return zpos_property_;
381}
382
Sean Paulf72cccd2018-08-27 13:59:08 -0400383} // namespace android