blob: fd08379a812580ae7412149ca101ae89834aaa2a [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_);
138 if (ret)
139 ALOGI("Could not get pixel blend mode property");
140
Robert Fossa09220c2016-09-30 10:27:23 -0400141 ret = drm_->GetPlaneProperty(*this, "IN_FENCE_FD", &in_fence_fd_property_);
142 if (ret)
143 ALOGI("Could not get IN_FENCE_FD property");
144
Matvii Zorin8338c342020-09-08 16:12:51 +0300145 if (HasNonRgbFormat()) {
146 ret = drm_->GetPlaneProperty(*this, "COLOR_ENCODING",
147 &color_encoding_propery_);
148 if (ret)
149 ALOGI("Could not get COLOR_ENCODING property");
150
151 ret = drm_->GetPlaneProperty(*this, "COLOR_RANGE", &color_range_property_);
152 if (ret)
153 ALOGI("Could not get COLOR_RANGE property");
154 }
155
Sean Paul6a55e9f2015-04-30 15:31:06 -0400156 return 0;
157}
158
159uint32_t DrmPlane::id() const {
160 return id_;
161}
162
163bool DrmPlane::GetCrtcSupported(const DrmCrtc &crtc) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200164 return ((1 << crtc.pipe()) & possible_crtc_mask_) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165}
166
Matvii Zorin67a89d32021-01-31 14:45:05 +0200167bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
Benjamin Lib00b28d2021-05-09 08:52:18 -0700168 if (rotation_property_.id() == 0) {
169 if (layer->transform != DrmHwcTransform::kIdentity) {
170 ALOGV("Rotation is not supported on plane %d", id_);
171 return false;
172 }
173 } else {
174 // For rotation checks, we assume the hardware reports its capabilities
175 // consistently (e.g. a 270 degree rotation is a 90 degree rotation + H
176 // flip + V flip; it wouldn't make sense to support all of the latter but
177 // not the former).
178 int ret = 0;
179 const std::pair<enum DrmHwcTransform, std::string> transforms[] =
180 {{kFlipH, "reflect-x"},
181 {kFlipV, "reflect-y"},
182 {kRotate90, "rotate-90"},
183 {kRotate180, "rotate-180"},
184 {kRotate270, "rotate-270"}};
185
186 for (const auto &[transform, name] : transforms) {
187 if (layer->transform & transform) {
188 std::tie(std::ignore,
189 ret) = rotation_property_.GetEnumValueWithName(name);
190 if (ret) {
191 ALOGV("Rotation '%s' is not supported on plane %d", name.c_str(),
192 id_);
193 return false;
194 }
195 }
196 }
Matvii Zorin67a89d32021-01-31 14:45:05 +0200197 }
198
199 if (alpha_property_.id() == 0 && layer->alpha != 0xffff) {
200 ALOGV("Alpha is not supported on plane %d", id_);
201 return false;
202 }
203
204 if (blend_property_.id() == 0) {
205 if ((layer->blending != DrmHwcBlending::kNone) &&
206 (layer->blending != DrmHwcBlending::kPreMult)) {
207 ALOGV("Blending is not supported on plane %d", id_);
208 return false;
209 }
210 } else {
211 int ret = 0;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200212
213 switch (layer->blending) {
214 case DrmHwcBlending::kPreMult:
Benjamin Lif75099a2021-05-09 08:52:35 -0700215 std::tie(std::ignore,
Matvii Zorin67a89d32021-01-31 14:45:05 +0200216 ret) = blend_property_.GetEnumValueWithName("Pre-multiplied");
217 break;
218 case DrmHwcBlending::kCoverage:
Benjamin Lif75099a2021-05-09 08:52:35 -0700219 std::tie(std::ignore,
220 ret) = blend_property_.GetEnumValueWithName("Coverage");
Matvii Zorin67a89d32021-01-31 14:45:05 +0200221 break;
222 case DrmHwcBlending::kNone:
223 default:
Benjamin Lif75099a2021-05-09 08:52:35 -0700224 std::tie(std::ignore,
225 ret) = blend_property_.GetEnumValueWithName("None");
Matvii Zorin67a89d32021-01-31 14:45:05 +0200226 break;
227 }
228 if (ret) {
229 ALOGV("Expected a valid blend mode on plane %d", id_);
230 return false;
231 }
232 }
233
Roman Stratiienko51287152021-02-10 14:59:52 +0200234 uint32_t format = layer->buffer_info.format;
Matvii Zorin67a89d32021-01-31 14:45:05 +0200235 if (!IsFormatSupported(format)) {
236 ALOGV("Plane %d does not supports %c%c%c%c format", id_, format,
237 format >> 8, format >> 16, format >> 24);
238 return false;
239 }
240
241 return true;
242}
243
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244uint32_t DrmPlane::type() const {
245 return type_;
246}
247
Roman Kovalivskyi859b6072020-03-26 05:03:39 +0200248bool DrmPlane::IsFormatSupported(uint32_t format) const {
249 return std::find(std::begin(formats_), std::end(formats_), format) !=
250 std::end(formats_);
251}
252
Matvii Zorin8338c342020-09-08 16:12:51 +0300253bool DrmPlane::HasNonRgbFormat() const {
254 return std::find_if_not(std::begin(formats_), std::end(formats_),
255 [](uint32_t format) {
256 return BufferInfoGetter::IsDrmFormatRgb(format);
257 }) != std::end(formats_);
258}
259
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300260auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
261 uint32_t zpos, uint32_t crtc_id) -> int {
262 int ret = 0;
263 uint32_t fb_id = UINT32_MAX;
264 int fence_fd = -1;
265 hwc_rect_t display_frame;
266 hwc_frect_t source_crop;
267 uint64_t rotation = 0;
268 uint64_t alpha = 0xFFFF;
269 uint64_t blend = UINT64_MAX;
270 uint64_t color_encoding = UINT64_MAX;
271 uint64_t color_range = UINT64_MAX;
272
273 if (!layer.FbIdHandle) {
274 ALOGE("Expected a valid framebuffer for pset");
275 return -EINVAL;
276 }
277
278 fb_id = layer.FbIdHandle->GetFbId();
279 fence_fd = layer.acquire_fence.Get();
280 display_frame = layer.display_frame;
281 source_crop = layer.source_crop;
282 alpha = layer.alpha;
283
284 // Disable the plane if there's no framebuffer
285 if (fb_id == UINT32_MAX) {
286 if (!AtomicDisablePlane(pset)) {
287 return -EINVAL;
288 }
289 return 0;
290 }
291
292 if (blend_property_) {
293 switch (layer.blending) {
294 case DrmHwcBlending::kPreMult:
295 std::tie(blend,
296 ret) = blend_property_.GetEnumValueWithName("Pre-multiplied");
297 break;
298 case DrmHwcBlending::kCoverage:
299 std::tie(blend, ret) = blend_property_.GetEnumValueWithName("Coverage");
300 break;
301 case DrmHwcBlending::kNone:
302 default:
303 std::tie(blend, ret) = blend_property_.GetEnumValueWithName("None");
304 break;
305 }
306 }
307
308 if (zpos_property_ && !zpos_property_.is_immutable()) {
309 uint64_t min_zpos = 0;
310
311 // Ignore ret and use min_zpos as 0 by default
312 std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
313
314 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
315 return -EINVAL;
316 }
317 }
318
319 rotation = 0;
320 if (layer.transform & DrmHwcTransform::kFlipH)
321 rotation |= DRM_MODE_REFLECT_X;
322 if (layer.transform & DrmHwcTransform::kFlipV)
323 rotation |= DRM_MODE_REFLECT_Y;
324 if (layer.transform & DrmHwcTransform::kRotate90)
325 rotation |= DRM_MODE_ROTATE_90;
326 else if (layer.transform & DrmHwcTransform::kRotate180)
327 rotation |= DRM_MODE_ROTATE_180;
328 else if (layer.transform & DrmHwcTransform::kRotate270)
329 rotation |= DRM_MODE_ROTATE_270;
330 else
331 rotation |= DRM_MODE_ROTATE_0;
332
333 if (fence_fd >= 0) {
334 if (!in_fence_fd_property_.AtomicSet(pset, fence_fd)) {
335 return -EINVAL;
336 }
337 }
338
339 if (color_encoding_propery_) {
340 switch (layer.dataspace & HAL_DATASPACE_STANDARD_MASK) {
341 case HAL_DATASPACE_STANDARD_BT709:
342 std::tie(color_encoding,
343 ret) = color_encoding_propery_
344 .GetEnumValueWithName("ITU-R BT.709 YCbCr");
345 break;
346 case HAL_DATASPACE_STANDARD_BT601_625:
347 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
348 case HAL_DATASPACE_STANDARD_BT601_525:
349 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
350 std::tie(color_encoding,
351 ret) = color_encoding_propery_
352 .GetEnumValueWithName("ITU-R BT.601 YCbCr");
353 break;
354 case HAL_DATASPACE_STANDARD_BT2020:
355 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
356 std::tie(color_encoding,
357 ret) = color_encoding_propery_
358 .GetEnumValueWithName("ITU-R BT.2020 YCbCr");
359 break;
360 }
361 }
362
363 if (color_range_property_) {
364 switch (layer.dataspace & HAL_DATASPACE_RANGE_MASK) {
365 case HAL_DATASPACE_RANGE_FULL:
366 std::tie(color_range, ret) = color_range_property_.GetEnumValueWithName(
367 "YCbCr full range");
368 break;
369 case HAL_DATASPACE_RANGE_LIMITED:
370 std::tie(color_range, ret) = color_range_property_.GetEnumValueWithName(
371 "YCbCr limited range");
372 break;
373 }
374 }
375
376 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
377 !fb_property_.AtomicSet(pset, fb_id) ||
378 !crtc_x_property_.AtomicSet(pset, display_frame.left) ||
379 !crtc_y_property_.AtomicSet(pset, display_frame.top) ||
380 !crtc_w_property_.AtomicSet(pset,
381 display_frame.right - display_frame.left) ||
382 !crtc_h_property_.AtomicSet(pset,
383 display_frame.bottom - display_frame.top) ||
384 !src_x_property_.AtomicSet(pset, (int)(source_crop.left) << 16) ||
385 !src_y_property_.AtomicSet(pset, (int)(source_crop.top) << 16) ||
386 !src_w_property_.AtomicSet(pset,
387 (int)(source_crop.right - source_crop.left)
388 << 16) ||
389 !src_h_property_.AtomicSet(pset,
390 (int)(source_crop.bottom - source_crop.top)
391 << 16)) {
392 return -EINVAL;
393 }
394
395 if (rotation_property_) {
396 if (!rotation_property_.AtomicSet(pset, rotation)) {
397 return -EINVAL;
398 }
399 }
400
401 if (alpha_property_) {
402 if (!alpha_property_.AtomicSet(pset, alpha)) {
403 return -EINVAL;
404 }
405 }
406
407 if (blend_property_ && blend != UINT64_MAX) {
408 if (!blend_property_.AtomicSet(pset, blend)) {
409 return -EINVAL;
410 }
411 }
412
413 if (color_encoding_propery_ && color_encoding != UINT64_MAX) {
414 if (!color_encoding_propery_.AtomicSet(pset, color_encoding)) {
415 return -EINVAL;
416 }
417 }
418
419 if (color_range_property_ && color_range != UINT64_MAX) {
420 if (!color_range_property_.AtomicSet(pset, color_range)) {
421 return -EINVAL;
422 }
423 }
424 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400425}
426
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300427auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
428 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
429 return -EINVAL;
430 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400431
Roman Stratiienko0dbe6392021-09-29 12:47:16 +0300432 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400433}
Sean Paul1c4c3262015-07-14 15:51:52 -0400434
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100435const DrmProperty &DrmPlane::zpos_property() const {
436 return zpos_property_;
437}
438
Sean Paulf72cccd2018-08-27 13:59:08 -0400439} // namespace android