1 /*
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
19 #include "DrmPlane.h"
20
21 #include <algorithm>
22 #include <cerrno>
23 #include <cinttypes>
24 #include <cstdint>
25
26 #include "DrmDevice.h"
27 #include "bufferinfo/BufferInfoGetter.h"
28 #include "utils/log.h"
29
30 namespace android {
31
CreateInstance(DrmDevice & dev,uint32_t plane_id)32 auto DrmPlane::CreateInstance(DrmDevice &dev, uint32_t plane_id)
33 -> std::unique_ptr<DrmPlane> {
34 auto p = MakeDrmModePlaneUnique(*dev.GetFd(), plane_id);
35 if (!p) {
36 ALOGE("Failed to get plane %d", plane_id);
37 return {};
38 }
39
40 auto plane = std::unique_ptr<DrmPlane>(new DrmPlane(dev, std::move(p)));
41
42 if (plane->Init() != 0) {
43 ALOGE("Failed to init plane %d", plane_id);
44 return {};
45 }
46
47 return plane;
48 }
49
Init()50 int DrmPlane::Init() {
51 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
52 formats_ = {plane_->formats, plane_->formats + plane_->count_formats};
53
54 DrmProperty p;
55
56 if (!GetPlaneProperty("type", p)) {
57 return -ENOTSUP;
58 }
59
60 auto type = p.GetValue();
61 if (!type) {
62 ALOGE("Failed to get plane type property value");
63 return -EINVAL;
64 }
65 switch (*type) {
66 case DRM_PLANE_TYPE_OVERLAY:
67 case DRM_PLANE_TYPE_PRIMARY:
68 case DRM_PLANE_TYPE_CURSOR:
69 type_ = (uint32_t)*type;
70 break;
71 default:
72 ALOGE("Invalid plane type %" PRIu64, *type);
73 return -EINVAL;
74 }
75
76 if (!GetPlaneProperty("CRTC_ID", crtc_property_) ||
77 !GetPlaneProperty("FB_ID", fb_property_) ||
78 !GetPlaneProperty("CRTC_X", crtc_x_property_) ||
79 !GetPlaneProperty("CRTC_Y", crtc_y_property_) ||
80 !GetPlaneProperty("CRTC_W", crtc_w_property_) ||
81 !GetPlaneProperty("CRTC_H", crtc_h_property_) ||
82 !GetPlaneProperty("SRC_X", src_x_property_) ||
83 !GetPlaneProperty("SRC_Y", src_y_property_) ||
84 !GetPlaneProperty("SRC_W", src_w_property_) ||
85 !GetPlaneProperty("SRC_H", src_h_property_)) {
86 return -ENOTSUP;
87 }
88
89 GetPlaneProperty("zpos", zpos_property_, Presence::kOptional);
90
91 /* DRM/KMS uses counter-clockwise rotations, while HWC API uses
92 * clockwise. That's why 90 and 270 are swapped here.
93 */
94 if (GetPlaneProperty("rotation", rotation_property_, Presence::kOptional)) {
95 rotation_property_.AddEnumToMap("rotate-0", LayerTransform::kIdentity,
96 transform_enum_map_);
97 rotation_property_.AddEnumToMap("rotate-90", LayerTransform::kRotate270,
98 transform_enum_map_);
99 rotation_property_.AddEnumToMap("rotate-180", LayerTransform::kRotate180,
100 transform_enum_map_);
101 rotation_property_.AddEnumToMap("rotate-270", LayerTransform::kRotate90,
102 transform_enum_map_);
103 rotation_property_.AddEnumToMap("reflect-x", LayerTransform::kFlipH,
104 transform_enum_map_);
105 rotation_property_.AddEnumToMap("reflect-y", LayerTransform::kFlipV,
106 transform_enum_map_);
107 }
108
109 GetPlaneProperty("alpha", alpha_property_, Presence::kOptional);
110
111 if (GetPlaneProperty("pixel blend mode", blend_property_,
112 Presence::kOptional)) {
113 blend_property_.AddEnumToMap("Pre-multiplied", BufferBlendMode::kPreMult,
114 blending_enum_map_);
115 blend_property_.AddEnumToMap("Coverage", BufferBlendMode::kCoverage,
116 blending_enum_map_);
117 blend_property_.AddEnumToMap("None", BufferBlendMode::kNone,
118 blending_enum_map_);
119 }
120
121 GetPlaneProperty("IN_FENCE_FD", in_fence_fd_property_, Presence::kOptional);
122
123 if (HasNonRgbFormat()) {
124 if (GetPlaneProperty("COLOR_ENCODING", color_encoding_propery_,
125 Presence::kOptional)) {
126 color_encoding_propery_.AddEnumToMap("ITU-R BT.709 YCbCr",
127 BufferColorSpace::kItuRec709,
128 color_encoding_enum_map_);
129 color_encoding_propery_.AddEnumToMap("ITU-R BT.601 YCbCr",
130 BufferColorSpace::kItuRec601,
131 color_encoding_enum_map_);
132 color_encoding_propery_.AddEnumToMap("ITU-R BT.2020 YCbCr",
133 BufferColorSpace::kItuRec2020,
134 color_encoding_enum_map_);
135 }
136
137 if (GetPlaneProperty("COLOR_RANGE", color_range_property_,
138 Presence::kOptional)) {
139 color_range_property_.AddEnumToMap("YCbCr full range",
140 BufferSampleRange::kFullRange,
141 color_range_enum_map_);
142 color_range_property_.AddEnumToMap("YCbCr limited range",
143 BufferSampleRange::kLimitedRange,
144 color_range_enum_map_);
145 }
146 }
147
148 return 0;
149 }
150
IsCrtcSupported(const DrmCrtc & crtc) const151 bool DrmPlane::IsCrtcSupported(const DrmCrtc &crtc) const {
152 auto crtc_prop_optval = crtc_property_.GetValue();
153 auto crtc_prop_val = crtc_prop_optval ? *crtc_prop_optval : 0;
154
155 if (crtc_prop_val != 0 && crtc_prop_val != crtc.GetId() &&
156 GetType() == DRM_PLANE_TYPE_PRIMARY) {
157 // Some DRM driver such as omap_drm allows sharing primary plane between
158 // CRTCs, but the primay plane could not be shared if it has been used by
159 // any CRTC already, which is protected by the plane_switching_crtc function
160 // in the kernel drivers/gpu/drm/drm_atomic.c file.
161 // The current drm_hwc design is not ready to support such scenario yet,
162 // so adding the CRTC status check here to workaorund for now.
163 ALOGW("%s: This Plane(id=%d) is activated for Crtc(id=%" PRIu64
164 "), could not be used for Crtc (id=%d)",
165 __FUNCTION__, GetId(), crtc_prop_val, crtc.GetId());
166 return false;
167 }
168
169 return ((1 << crtc.GetIndexInResArray()) & plane_->possible_crtcs) != 0;
170 }
171
IsValidForLayer(LayerData * layer)172 bool DrmPlane::IsValidForLayer(LayerData *layer) {
173 if (layer == nullptr || !layer->bi) {
174 ALOGE("%s: Invalid parameters", __func__);
175 return false;
176 }
177
178 if (!rotation_property_) {
179 if (layer->pi.transform != LayerTransform::kIdentity) {
180 ALOGV("No rotation property on plane %d", GetId());
181 return false;
182 }
183 } else {
184 if (transform_enum_map_.count(layer->pi.transform) == 0) {
185 ALOGV("Transform is not supported on plane %d", GetId());
186 return false;
187 }
188 }
189
190 if (!alpha_property_ && layer->pi.alpha != UINT16_MAX) {
191 ALOGV("Alpha is not supported on plane %d", GetId());
192 return false;
193 }
194
195 if (blending_enum_map_.count(layer->bi->blend_mode) == 0 &&
196 layer->bi->blend_mode != BufferBlendMode::kNone &&
197 layer->bi->blend_mode != BufferBlendMode::kPreMult) {
198 ALOGV("Blending is not supported on plane %d", GetId());
199 return false;
200 }
201
202 auto format = layer->bi->format;
203 if (!IsFormatSupported(format)) {
204 ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
205 format >> 8, format >> 16, format >> 24);
206 return false;
207 }
208
209 return true;
210 }
211
IsFormatSupported(uint32_t format) const212 bool DrmPlane::IsFormatSupported(uint32_t format) const {
213 return std::find(std::begin(formats_), std::end(formats_), format) !=
214 std::end(formats_);
215 }
216
HasNonRgbFormat() const217 bool DrmPlane::HasNonRgbFormat() const {
218 return std::find_if_not(std::begin(formats_), std::end(formats_),
219 [](uint32_t format) {
220 return BufferInfoGetter::IsDrmFormatRgb(format);
221 }) != std::end(formats_);
222 }
223
ToDrmRotation(LayerTransform transform)224 static uint64_t ToDrmRotation(LayerTransform transform) {
225 uint64_t rotation = 0;
226 /* DRM/KMS uses counter-clockwise rotations, while HWC API uses
227 * clockwise. That's why 90 and 270 are swapped here.
228 */
229 if ((transform & LayerTransform::kFlipH) != 0)
230 rotation |= DRM_MODE_REFLECT_X;
231 if ((transform & LayerTransform::kFlipV) != 0)
232 rotation |= DRM_MODE_REFLECT_Y;
233 if ((transform & LayerTransform::kRotate90) != 0)
234 rotation |= DRM_MODE_ROTATE_270;
235 else if ((transform & LayerTransform::kRotate180) != 0)
236 rotation |= DRM_MODE_ROTATE_180;
237 else if ((transform & LayerTransform::kRotate270) != 0)
238 rotation |= DRM_MODE_ROTATE_90;
239 else
240 rotation |= DRM_MODE_ROTATE_0;
241
242 return rotation;
243 }
244
245 /* Convert float to 16.16 fixed point */
To1616FixPt(float in)246 static int To1616FixPt(float in) {
247 constexpr int kBitShift = 16;
248 return int(in * (1 << kBitShift));
249 }
250
AtomicSetState(drmModeAtomicReq & pset,LayerData & layer,uint32_t zpos,uint32_t crtc_id)251 auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, LayerData &layer,
252 uint32_t zpos, uint32_t crtc_id) -> int {
253 if (!layer.fb || !layer.bi) {
254 ALOGE("%s: Invalid arguments", __func__);
255 return -EINVAL;
256 }
257
258 if (zpos_property_ && !zpos_property_.IsImmutable()) {
259 uint64_t min_zpos = 0;
260
261 // Ignore ret and use min_zpos as 0 by default
262 std::tie(std::ignore, min_zpos) = zpos_property_.RangeMin();
263
264 if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
265 return -EINVAL;
266 }
267 }
268
269 if (layer.acquire_fence &&
270 !in_fence_fd_property_.AtomicSet(pset, *layer.acquire_fence)) {
271 return -EINVAL;
272 }
273
274 auto &disp = layer.pi.display_frame;
275 auto &src = layer.pi.source_crop;
276 if (!crtc_property_.AtomicSet(pset, crtc_id) ||
277 !fb_property_.AtomicSet(pset, layer.fb->GetFbId()) ||
278 !crtc_x_property_.AtomicSet(pset, disp.left) ||
279 !crtc_y_property_.AtomicSet(pset, disp.top) ||
280 !crtc_w_property_.AtomicSet(pset, disp.right - disp.left) ||
281 !crtc_h_property_.AtomicSet(pset, disp.bottom - disp.top) ||
282 !src_x_property_.AtomicSet(pset, To1616FixPt(src.left)) ||
283 !src_y_property_.AtomicSet(pset, To1616FixPt(src.top)) ||
284 !src_w_property_.AtomicSet(pset, To1616FixPt(src.right - src.left)) ||
285 !src_h_property_.AtomicSet(pset, To1616FixPt(src.bottom - src.top))) {
286 return -EINVAL;
287 }
288
289 if (rotation_property_ &&
290 !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.pi.transform))) {
291 return -EINVAL;
292 }
293
294 if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.pi.alpha)) {
295 return -EINVAL;
296 }
297
298 if (blending_enum_map_.count(layer.bi->blend_mode) != 0 &&
299 !blend_property_.AtomicSet(pset,
300 blending_enum_map_[layer.bi->blend_mode])) {
301 return -EINVAL;
302 }
303
304 if (color_encoding_enum_map_.count(layer.bi->color_space) != 0 &&
305 !color_encoding_propery_
306 .AtomicSet(pset, color_encoding_enum_map_[layer.bi->color_space])) {
307 return -EINVAL;
308 }
309
310 if (color_range_enum_map_.count(layer.bi->sample_range) != 0 &&
311 !color_range_property_
312 .AtomicSet(pset, color_range_enum_map_[layer.bi->sample_range])) {
313 return -EINVAL;
314 }
315
316 return 0;
317 }
318
AtomicDisablePlane(drmModeAtomicReq & pset)319 auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
320 if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
321 return -EINVAL;
322 }
323
324 return 0;
325 }
326
GetPlaneProperty(const char * prop_name,DrmProperty & property,Presence presence)327 auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
328 Presence presence) -> bool {
329 auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_PLANE, prop_name,
330 &property);
331 if (err != 0) {
332 if (presence == Presence::kMandatory) {
333 ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
334 GetId());
335 } else {
336 ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
337 GetId());
338 }
339 return false;
340 }
341
342 return true;
343 }
344
345 } // namespace android
346