• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", DrmHwcTransform::kIdentity,
95                                     transform_enum_map_);
96     rotation_property_.AddEnumToMap("rotate-90", DrmHwcTransform::kRotate90,
97                                     transform_enum_map_);
98     rotation_property_.AddEnumToMap("rotate-180", DrmHwcTransform::kRotate180,
99                                     transform_enum_map_);
100     rotation_property_.AddEnumToMap("rotate-270", DrmHwcTransform::kRotate270,
101                                     transform_enum_map_);
102     rotation_property_.AddEnumToMap("reflect-x", DrmHwcTransform::kFlipH,
103                                     transform_enum_map_);
104     rotation_property_.AddEnumToMap("reflect-y", DrmHwcTransform::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", DrmHwcBlending::kPreMult,
113                                  blending_enum_map_);
114     blend_property_.AddEnumToMap("Coverage", DrmHwcBlending::kCoverage,
115                                  blending_enum_map_);
116     blend_property_.AddEnumToMap("None", DrmHwcBlending::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                                            DrmHwcColorSpace::kItuRec709,
127                                            color_encoding_enum_map_);
128       color_encoding_propery_.AddEnumToMap("ITU-R BT.601 YCbCr",
129                                            DrmHwcColorSpace::kItuRec601,
130                                            color_encoding_enum_map_);
131       color_encoding_propery_.AddEnumToMap("ITU-R BT.2020 YCbCr",
132                                            DrmHwcColorSpace::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                                          DrmHwcSampleRange::kFullRange,
140                                          color_range_enum_map_);
141       color_range_property_.AddEnumToMap("YCbCr limited range",
142                                          DrmHwcSampleRange::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   return ((1 << crtc.GetIndexInResArray()) & plane_->possible_crtcs) != 0;
152 }
153 
IsValidForLayer(DrmHwcLayer * layer)154 bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
155   if (!rotation_property_) {
156     if (layer->transform != DrmHwcTransform::kIdentity) {
157       ALOGV("No rotation property on plane %d", GetId());
158       return false;
159     }
160   } else {
161     if (transform_enum_map_.count(layer->transform) == 0) {
162       ALOGV("Transform is not supported on plane %d", GetId());
163       return false;
164     }
165   }
166 
167   if (alpha_property_.id() == 0 && layer->alpha != UINT16_MAX) {
168     ALOGV("Alpha is not supported on plane %d", GetId());
169     return false;
170   }
171 
172   if (blending_enum_map_.count(layer->blending) == 0 &&
173       layer->blending != DrmHwcBlending::kNone &&
174       layer->blending != DrmHwcBlending::kPreMult) {
175     ALOGV("Blending is not supported on plane %d", GetId());
176     return false;
177   }
178 
179   uint32_t format = layer->buffer_info.format;
180   if (!IsFormatSupported(format)) {
181     ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
182           format >> 8, format >> 16, format >> 24);
183     return false;
184   }
185 
186   return true;
187 }
188 
IsFormatSupported(uint32_t format) const189 bool DrmPlane::IsFormatSupported(uint32_t format) const {
190   return std::find(std::begin(formats_), std::end(formats_), format) !=
191          std::end(formats_);
192 }
193 
HasNonRgbFormat() const194 bool DrmPlane::HasNonRgbFormat() const {
195   return std::find_if_not(std::begin(formats_), std::end(formats_),
196                           [](uint32_t format) {
197                             return BufferInfoGetter::IsDrmFormatRgb(format);
198                           }) != std::end(formats_);
199 }
200 
ToDrmRotation(DrmHwcTransform transform)201 static uint64_t ToDrmRotation(DrmHwcTransform transform) {
202   uint64_t rotation = 0;
203   if ((transform & DrmHwcTransform::kFlipH) != 0)
204     rotation |= DRM_MODE_REFLECT_X;
205   if ((transform & DrmHwcTransform::kFlipV) != 0)
206     rotation |= DRM_MODE_REFLECT_Y;
207   if ((transform & DrmHwcTransform::kRotate90) != 0)
208     rotation |= DRM_MODE_ROTATE_90;
209   else if ((transform & DrmHwcTransform::kRotate180) != 0)
210     rotation |= DRM_MODE_ROTATE_180;
211   else if ((transform & DrmHwcTransform::kRotate270) != 0)
212     rotation |= DRM_MODE_ROTATE_270;
213   else
214     rotation |= DRM_MODE_ROTATE_0;
215 
216   return rotation;
217 }
218 
219 /* Convert float to 16.16 fixed point */
To1616FixPt(float in)220 static int To1616FixPt(float in) {
221   constexpr int kBitShift = 16;
222   return int(in * (1 << kBitShift));
223 }
224 
AtomicSetState(drmModeAtomicReq & pset,DrmHwcLayer & layer,uint32_t zpos,uint32_t crtc_id)225 auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, DrmHwcLayer &layer,
226                               uint32_t zpos, uint32_t crtc_id) -> int {
227   if (!layer.fb_id_handle) {
228     ALOGE("Expected a valid framebuffer for pset");
229     return -EINVAL;
230   }
231 
232   if (zpos_property_ && !zpos_property_.is_immutable()) {
233     uint64_t min_zpos = 0;
234 
235     // Ignore ret and use min_zpos as 0 by default
236     std::tie(std::ignore, min_zpos) = zpos_property_.range_min();
237 
238     if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
239       return -EINVAL;
240     }
241   }
242 
243   if (layer.acquire_fence &&
244       !in_fence_fd_property_.AtomicSet(pset, layer.acquire_fence.Get())) {
245     return -EINVAL;
246   }
247 
248   if (!crtc_property_.AtomicSet(pset, crtc_id) ||
249       !fb_property_.AtomicSet(pset, layer.fb_id_handle->GetFbId()) ||
250       !crtc_x_property_.AtomicSet(pset, layer.display_frame.left) ||
251       !crtc_y_property_.AtomicSet(pset, layer.display_frame.top) ||
252       !crtc_w_property_.AtomicSet(pset, layer.display_frame.right -
253                                             layer.display_frame.left) ||
254       !crtc_h_property_.AtomicSet(pset, layer.display_frame.bottom -
255                                             layer.display_frame.top) ||
256       !src_x_property_.AtomicSet(pset, To1616FixPt(layer.source_crop.left)) ||
257       !src_y_property_.AtomicSet(pset, To1616FixPt(layer.source_crop.top)) ||
258       !src_w_property_.AtomicSet(pset, To1616FixPt(layer.source_crop.right -
259                                                    layer.source_crop.left)) ||
260       !src_h_property_.AtomicSet(pset, To1616FixPt(layer.source_crop.bottom -
261                                                    layer.source_crop.top))) {
262     return -EINVAL;
263   }
264 
265   if (rotation_property_ &&
266       !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.transform))) {
267     return -EINVAL;
268   }
269 
270   if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.alpha)) {
271     return -EINVAL;
272   }
273 
274   if (blending_enum_map_.count(layer.blending) != 0 &&
275       !blend_property_.AtomicSet(pset, blending_enum_map_[layer.blending])) {
276     return -EINVAL;
277   }
278 
279   if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
280       !color_encoding_propery_
281            .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
282     return -EINVAL;
283   }
284 
285   if (color_range_enum_map_.count(layer.sample_range) != 0 &&
286       !color_range_property_
287            .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
288     return -EINVAL;
289   }
290 
291   return 0;
292 }
293 
AtomicDisablePlane(drmModeAtomicReq & pset)294 auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
295   if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
296     return -EINVAL;
297   }
298 
299   return 0;
300 }
301 
GetPlaneProperty(const char * prop_name,DrmProperty & property,Presence presence)302 auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
303                                 Presence presence) -> bool {
304   int err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_PLANE, prop_name,
305                               &property);
306   if (err != 0) {
307     if (presence == Presence::kMandatory) {
308       ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
309             GetId());
310     } else {
311       ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
312             GetId());
313     }
314     return false;
315   }
316 
317   return true;
318 }
319 
320 }  // namespace android
321