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