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