aboutsummaryrefslogtreecommitdiff
path: root/drm/DrmPlane.cpp
blob: 228e3ddd954e30fa45dbd0d491872d44d4885102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "hwc-drm-plane"

#include "DrmPlane.h"

#include <algorithm>
#include <cerrno>
#include <cinttypes>
#include <cstdint>

#include "DrmDevice.h"
#include "bufferinfo/BufferInfoGetter.h"
#include "utils/log.h"

namespace android {

auto DrmPlane::CreateInstance(DrmDevice &dev, uint32_t plane_id)
    -> std::unique_ptr<DrmPlane> {
  auto p = MakeDrmModePlaneUnique(*dev.GetFd(), plane_id);
  if (!p) {
    ALOGE("Failed to get plane %d", plane_id);
    return {};
  }

  auto plane = std::unique_ptr<DrmPlane>(new DrmPlane(dev, std::move(p)));

  if (plane->Init() != 0) {
    ALOGE("Failed to init plane %d", plane_id);
    return {};
  }

  return plane;
}

int DrmPlane::Init() {
  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
  formats_ = {plane_->formats, plane_->formats + plane_->count_formats};

  DrmProperty p;

  if (!GetPlaneProperty("type", p)) {
    return -ENOTSUP;
  }

  auto type = p.GetValue();
  if (!type) {
    ALOGE("Failed to get plane type property value");
    return -EINVAL;
  }
  switch (*type) {
    case DRM_PLANE_TYPE_OVERLAY:
    case DRM_PLANE_TYPE_PRIMARY:
    case DRM_PLANE_TYPE_CURSOR:
      type_ = (uint32_t)*type;
      break;
    default:
      ALOGE("Invalid plane type %" PRIu64, *type);
      return -EINVAL;
  }

  if (!GetPlaneProperty("CRTC_ID", crtc_property_) ||
      !GetPlaneProperty("FB_ID", fb_property_) ||
      !GetPlaneProperty("CRTC_X", crtc_x_property_) ||
      !GetPlaneProperty("CRTC_Y", crtc_y_property_) ||
      !GetPlaneProperty("CRTC_W", crtc_w_property_) ||
      !GetPlaneProperty("CRTC_H", crtc_h_property_) ||
      !GetPlaneProperty("SRC_X", src_x_property_) ||
      !GetPlaneProperty("SRC_Y", src_y_property_) ||
      !GetPlaneProperty("SRC_W", src_w_property_) ||
      !GetPlaneProperty("SRC_H", src_h_property_)) {
    return -ENOTSUP;
  }

  GetPlaneProperty("zpos", zpos_property_, Presence::kOptional);

  if (GetPlaneProperty("rotation", rotation_property_, Presence::kOptional)) {
    rotation_property_.AddEnumToMap("rotate-0", LayerTransform::kIdentity,
                                    transform_enum_map_);
    rotation_property_.AddEnumToMap("rotate-90", LayerTransform::kRotate90,
                                    transform_enum_map_);
    rotation_property_.AddEnumToMap("rotate-180", LayerTransform::kRotate180,
                                    transform_enum_map_);
    rotation_property_.AddEnumToMap("rotate-270", LayerTransform::kRotate270,
                                    transform_enum_map_);
    rotation_property_.AddEnumToMap("reflect-x", LayerTransform::kFlipH,
                                    transform_enum_map_);
    rotation_property_.AddEnumToMap("reflect-y", LayerTransform::kFlipV,
                                    transform_enum_map_);
  }

  GetPlaneProperty("alpha", alpha_property_, Presence::kOptional);

  if (GetPlaneProperty("pixel blend mode", blend_property_,
                       Presence::kOptional)) {
    blend_property_.AddEnumToMap("Pre-multiplied", BufferBlendMode::kPreMult,
                                 blending_enum_map_);
    blend_property_.AddEnumToMap("Coverage", BufferBlendMode::kCoverage,
                                 blending_enum_map_);
    blend_property_.AddEnumToMap("None", BufferBlendMode::kNone,
                                 blending_enum_map_);
  }

  GetPlaneProperty("IN_FENCE_FD", in_fence_fd_property_, Presence::kOptional);

  if (HasNonRgbFormat()) {
    if (GetPlaneProperty("COLOR_ENCODING", color_encoding_propery_,
                         Presence::kOptional)) {
      color_encoding_propery_.AddEnumToMap("ITU-R BT.709 YCbCr",
                                           BufferColorSpace::kItuRec709,
                                           color_encoding_enum_map_);
      color_encoding_propery_.AddEnumToMap("ITU-R BT.601 YCbCr",
                                           BufferColorSpace::kItuRec601,
                                           color_encoding_enum_map_);
      color_encoding_propery_.AddEnumToMap("ITU-R BT.2020 YCbCr",
                                           BufferColorSpace::kItuRec2020,
                                           color_encoding_enum_map_);
    }

    if (GetPlaneProperty("COLOR_RANGE", color_range_property_,
                         Presence::kOptional)) {
      color_range_property_.AddEnumToMap("YCbCr full range",
                                         BufferSampleRange::kFullRange,
                                         color_range_enum_map_);
      color_range_property_.AddEnumToMap("YCbCr limited range",
                                         BufferSampleRange::kLimitedRange,
                                         color_range_enum_map_);
    }
  }

  return 0;
}

bool DrmPlane::IsCrtcSupported(const DrmCrtc &crtc) const {
  auto crtc_prop_optval = crtc_property_.GetValue();
  auto crtc_prop_val = crtc_prop_optval ? *crtc_prop_optval : 0;

  if (crtc_prop_val != 0 && crtc_prop_val != crtc.GetId() &&
      GetType() == DRM_PLANE_TYPE_PRIMARY) {
    // Some DRM driver such as omap_drm allows sharing primary plane between
    // CRTCs, but the primay plane could not be shared if it has been used by
    // any CRTC already, which is protected by the plane_switching_crtc function
    // in the kernel drivers/gpu/drm/drm_atomic.c file.
    // The current drm_hwc design is not ready to support such scenario yet,
    // so adding the CRTC status check here to workaorund for now.
    ALOGW("%s: This Plane(id=%d) is activated for Crtc(id=%" PRIu64
          "), could not be used for Crtc (id=%d)",
          __FUNCTION__, GetId(), crtc_prop_val, crtc.GetId());
    return false;
  }

  return ((1 << crtc.GetIndexInResArray()) & plane_->possible_crtcs) != 0;
}

bool DrmPlane::IsValidForLayer(LayerData *layer) {
  if (layer == nullptr || !layer->bi) {
    ALOGE("%s: Invalid parameters", __func__);
    return false;
  }

  if (!rotation_property_) {
    if (layer->pi.transform != LayerTransform::kIdentity) {
      ALOGV("No rotation property on plane %d", GetId());
      return false;
    }
  } else {
    if (transform_enum_map_.count(layer->pi.transform) == 0) {
      ALOGV("Transform is not supported on plane %d", GetId());
      return false;
    }
  }

  if (!alpha_property_ && layer->pi.alpha != UINT16_MAX) {
    ALOGV("Alpha is not supported on plane %d", GetId());
    return false;
  }

  if (blending_enum_map_.count(layer->bi->blend_mode) == 0 &&
      layer->bi->blend_mode != BufferBlendMode::kNone &&
      layer->bi->blend_mode != BufferBlendMode::kPreMult) {
    ALOGV("Blending is not supported on plane %d", GetId());
    return false;
  }

  auto format = layer->bi->format;
  if (!IsFormatSupported(format)) {
    ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
          format >> 8, format >> 16, format >> 24);
    return false;
  }

  return true;
}

bool DrmPlane::IsFormatSupported(uint32_t format) const {
  return std::find(std::begin(formats_), std::end(formats_), format) !=
         std::end(formats_);
}

bool DrmPlane::HasNonRgbFormat() const {
  return std::find_if_not(std::begin(formats_), std::end(formats_),
                          [](uint32_t format) {
                            return BufferInfoGetter::IsDrmFormatRgb(format);
                          }) != std::end(formats_);
}

static uint64_t ToDrmRotation(LayerTransform transform) {
  uint64_t rotation = 0;
  if ((transform & LayerTransform::kFlipH) != 0)
    rotation |= DRM_MODE_REFLECT_X;
  if ((transform & LayerTransform::kFlipV) != 0)
    rotation |= DRM_MODE_REFLECT_Y;
  if ((transform & LayerTransform::kRotate90) != 0)
    rotation |= DRM_MODE_ROTATE_90;
  else if ((transform & LayerTransform::kRotate180) != 0)
    rotation |= DRM_MODE_ROTATE_180;
  else if ((transform & LayerTransform::kRotate270) != 0)
    rotation |= DRM_MODE_ROTATE_270;
  else
    rotation |= DRM_MODE_ROTATE_0;

  return rotation;
}

/* Convert float to 16.16 fixed point */
static int To1616FixPt(float in) {
  constexpr int kBitShift = 16;
  return int(in * (1 << kBitShift));
}

auto DrmPlane::AtomicSetState(drmModeAtomicReq &pset, LayerData &layer,
                              uint32_t zpos, uint32_t crtc_id) -> int {
  if (!layer.fb || !layer.bi) {
    ALOGE("%s: Invalid arguments", __func__);
    return -EINVAL;
  }

  if (zpos_property_ && !zpos_property_.IsImmutable()) {
    uint64_t min_zpos = 0;

    // Ignore ret and use min_zpos as 0 by default
    std::tie(std::ignore, min_zpos) = zpos_property_.RangeMin();

    if (!zpos_property_.AtomicSet(pset, zpos + min_zpos)) {
      return -EINVAL;
    }
  }

  if (layer.acquire_fence &&
      !in_fence_fd_property_.AtomicSet(pset, *layer.acquire_fence)) {
    return -EINVAL;
  }

  auto &disp = layer.pi.display_frame;
  auto &src = layer.pi.source_crop;
  if (!crtc_property_.AtomicSet(pset, crtc_id) ||
      !fb_property_.AtomicSet(pset, layer.fb->GetFbId()) ||
      !crtc_x_property_.AtomicSet(pset, disp.left) ||
      !crtc_y_property_.AtomicSet(pset, disp.top) ||
      !crtc_w_property_.AtomicSet(pset, disp.right - disp.left) ||
      !crtc_h_property_.AtomicSet(pset, disp.bottom - disp.top) ||
      !src_x_property_.AtomicSet(pset, To1616FixPt(src.left)) ||
      !src_y_property_.AtomicSet(pset, To1616FixPt(src.top)) ||
      !src_w_property_.AtomicSet(pset, To1616FixPt(src.right - src.left)) ||
      !src_h_property_.AtomicSet(pset, To1616FixPt(src.bottom - src.top))) {
    return -EINVAL;
  }

  if (rotation_property_ &&
      !rotation_property_.AtomicSet(pset, ToDrmRotation(layer.pi.transform))) {
    return -EINVAL;
  }

  if (alpha_property_ && !alpha_property_.AtomicSet(pset, layer.pi.alpha)) {
    return -EINVAL;
  }

  if (blending_enum_map_.count(layer.bi->blend_mode) != 0 &&
      !blend_property_.AtomicSet(pset,
                                 blending_enum_map_[layer.bi->blend_mode])) {
    return -EINVAL;
  }

  if (color_encoding_enum_map_.count(layer.bi->color_space) != 0 &&
      !color_encoding_propery_
           .AtomicSet(pset, color_encoding_enum_map_[layer.bi->color_space])) {
    return -EINVAL;
  }

  if (color_range_enum_map_.count(layer.bi->sample_range) != 0 &&
      !color_range_property_
           .AtomicSet(pset, color_range_enum_map_[layer.bi->sample_range])) {
    return -EINVAL;
  }

  return 0;
}

auto DrmPlane::AtomicDisablePlane(drmModeAtomicReq &pset) -> int {
  if (!crtc_property_.AtomicSet(pset, 0) || !fb_property_.AtomicSet(pset, 0)) {
    return -EINVAL;
  }

  return 0;
}

auto DrmPlane::GetPlaneProperty(const char *prop_name, DrmProperty &property,
                                Presence presence) -> bool {
  auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_PLANE, prop_name,
                               &property);
  if (err != 0) {
    if (presence == Presence::kMandatory) {
      ALOGE("Could not get mandatory property \"%s\" from plane %d", prop_name,
            GetId());
    } else {
      ALOGV("Could not get optional property \"%s\" from plane %d", prop_name,
            GetId());
    }
    return false;
  }

  return true;
}

}  // namespace android