xref: /aosp_15_r20/external/drm_hwcomposer/drm/DrmPlane.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
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 "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 primary 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     return false;
164   }
165 
166   return ((1 << crtc.GetIndexInResArray()) & plane_->possible_crtcs) != 0;
167 }
168 
IsValidForLayer(LayerData * layer)169 bool DrmPlane::IsValidForLayer(LayerData *layer) {
170   if (layer == nullptr || !layer->bi) {
171     ALOGE("%s: Invalid parameters", __func__);
172     return false;
173   }
174 
175   if (!rotation_property_) {
176     if (layer->pi.transform != LayerTransform::kIdentity) {
177       ALOGV("No rotation property on plane %d", GetId());
178       return false;
179     }
180   } else {
181     if (transform_enum_map_.count(layer->pi.transform) == 0) {
182       ALOGV("Transform is not supported on plane %d", GetId());
183       return false;
184     }
185   }
186 
187   if (!alpha_property_ && layer->pi.alpha != UINT16_MAX) {
188     ALOGV("Alpha is not supported on plane %d", GetId());
189     return false;
190   }
191 
192   if (blending_enum_map_.count(layer->bi->blend_mode) == 0 &&
193       layer->bi->blend_mode != BufferBlendMode::kNone &&
194       layer->bi->blend_mode != BufferBlendMode::kPreMult) {
195     ALOGV("Blending is not supported on plane %d", GetId());
196     return false;
197   }
198 
199   auto format = layer->bi->format;
200   if (!IsFormatSupported(format)) {
201     ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
202           format >> 8, format >> 16, format >> 24);
203     return false;
204   }
205 
206   return true;
207 }
208 
IsFormatSupported(uint32_t format) const209 bool DrmPlane::IsFormatSupported(uint32_t format) const {
210   return std::find(std::begin(formats_), std::end(formats_), format) !=
211          std::end(formats_);
212 }
213 
HasNonRgbFormat() const214 bool DrmPlane::HasNonRgbFormat() const {
215   return std::find_if_not(std::begin(formats_), std::end(formats_),
216                           [](uint32_t format) {
217                             return BufferInfoGetter::IsDrmFormatRgb(format);
218                           }) != std::end(formats_);
219 }
220 
ToDrmRotation(LayerTransform transform)221 static uint64_t ToDrmRotation(LayerTransform transform) {
222   uint64_t rotation = 0;
223   /* DRM/KMS uses counter-clockwise rotations, while HWC API uses
224    * clockwise. That's why 90 and 270 are swapped here.
225    */
226   if ((transform & LayerTransform::kFlipH) != 0)
227     rotation |= DRM_MODE_REFLECT_X;
228   if ((transform & LayerTransform::kFlipV) != 0)
229     rotation |= DRM_MODE_REFLECT_Y;
230   if ((transform & LayerTransform::kRotate90) != 0)
231     rotation |= DRM_MODE_ROTATE_270;
232   else if ((transform & LayerTransform::kRotate180) != 0)
233     rotation |= DRM_MODE_ROTATE_180;
234   else if ((transform & LayerTransform::kRotate270) != 0)
235     rotation |= DRM_MODE_ROTATE_90;
236   else
237     rotation |= DRM_MODE_ROTATE_0;
238 
239   return rotation;
240 }
241 
242 /* Convert float to 16.16 fixed point */
To1616FixPt(float in)243 static int To1616FixPt(float in) {
244   constexpr int kBitShift = 16;
245   return int(in * (1 << kBitShift));
246 }
247 
AtomicSetState(drmModeAtomicReq & pset,LayerData & layer,uint32_t zpos,uint32_t crtc_id)248 auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, LayerData &layer,
249                               uint32_t zpos, uint32_t crtc_id) -> int {
250   if (!layer.fb || !layer.bi) {
251     ALOGE("%s: Invalid arguments", __func__);
252     return -EINVAL;
253   }
254 
255   if (zpos_property_ && !zpos_property_.IsImmutable()) {
256     uint64_t min_zpos = 0;
257 
258     // Ignore ret and use min_zpos as 0 by default
259     std::tie(std::ignore, min_zpos) = zpos_property_.RangeMin();
260 
261     if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
262       return -EINVAL;
263     }
264   }
265 
266   if (layer.acquire_fence &&
267       !in_fence_fd_property_.AtomicSet(pset, *layer.acquire_fence)) {
268     return -EINVAL;
269   }
270 
271   auto &disp = layer.pi.display_frame;
272   auto &src = layer.pi.source_crop;
273   if (!crtc_property_.AtomicSet(pset, crtc_id) ||
274       !fb_property_.AtomicSet(pset, layer.fb->GetFbId()) ||
275       !crtc_x_property_.AtomicSet(pset, disp.left) ||
276       !crtc_y_property_.AtomicSet(pset, disp.top) ||
277       !crtc_w_property_.AtomicSet(pset, disp.right - disp.left) ||
278       !crtc_h_property_.AtomicSet(pset, disp.bottom - disp.top) ||
279       !src_x_property_.AtomicSet(pset, To1616FixPt(src.left)) ||
280       !src_y_property_.AtomicSet(pset, To1616FixPt(src.top)) ||
281       !src_w_property_.AtomicSet(pset, To1616FixPt(src.right - src.left)) ||
282       !src_h_property_.AtomicSet(pset, To1616FixPt(src.bottom - src.top))) {
283     return -EINVAL;
284   }
285 
286   if (rotation_property_ &&
287       !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.pi.transform))) {
288     return -EINVAL;
289   }
290 
291   if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.pi.alpha)) {
292     return -EINVAL;
293   }
294 
295   if (blending_enum_map_.count(layer.bi->blend_mode) != 0 &&
296       !blend_property_.AtomicSet(pset,
297                                  blending_enum_map_[layer.bi->blend_mode])) {
298     return -EINVAL;
299   }
300 
301   if (color_encoding_enum_map_.count(layer.bi->color_space) != 0 &&
302       !color_encoding_propery_
303            .AtomicSet(pset, color_encoding_enum_map_[layer.bi->color_space])) {
304     return -EINVAL;
305   }
306 
307   if (color_range_enum_map_.count(layer.bi->sample_range) != 0 &&
308       !color_range_property_
309            .AtomicSet(pset, color_range_enum_map_[layer.bi->sample_range])) {
310     return -EINVAL;
311   }
312 
313   return 0;
314 }
315 
AtomicDisablePlane(drmModeAtomicReq & pset)316 auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
317   if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
318     return -EINVAL;
319   }
320 
321   return 0;
322 }
323 
GetPlaneProperty(const char * prop_name,DrmProperty & property,Presence presence)324 auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
325                                 Presence presence) -> bool {
326   auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_PLANE, prop_name,
327                                &property);
328   if (err != 0) {
329     if (presence == Presence::kMandatory) {
330       ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
331             GetId());
332     } else {
333       ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
334             GetId());
335     }
336     return false;
337   }
338 
339   return true;
340 }
341 
342 }  // namespace android
343