Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 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 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 17 | #define LOG_TAG "hwc-drm-device" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 18 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include "DrmDevice.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 20 | |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdint.h> |
| 24 | #include <xf86drm.h> |
| 25 | #include <xf86drmMode.h> |
| 26 | |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <array> |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 29 | #include <cinttypes> |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 30 | #include <sstream> |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 31 | #include <string> |
| 32 | |
Roman Stratiienko | d518a05 | 2021-02-25 19:15:14 +0200 | [diff] [blame^] | 33 | #include "utils/log.h" |
| 34 | #include "utils/properties.h" |
| 35 | |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 36 | static void trim_left(std::string &str) { |
| 37 | str.erase(std::begin(str), |
| 38 | std::find_if(std::begin(str), std::end(str), |
| 39 | [](int ch) { return !std::isspace(ch); })); |
| 40 | } |
| 41 | |
| 42 | static void trim_right(std::string &str) { |
| 43 | str.erase(std::find_if(std::rbegin(str), std::rend(str), |
| 44 | [](int ch) { return !std::isspace(ch); }) |
| 45 | .base(), |
| 46 | std::end(str)); |
| 47 | } |
| 48 | |
| 49 | static void trim(std::string &str) { |
| 50 | trim_left(str); |
| 51 | trim_right(str); |
| 52 | } |
| 53 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 54 | namespace android { |
| 55 | |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 56 | static std::vector<std::string> read_primary_display_order_prop() { |
| 57 | std::array<char, PROPERTY_VALUE_MAX> display_order_buf; |
Jason Macnak | f1af957 | 2020-08-20 11:49:51 -0700 | [diff] [blame] | 58 | property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(), |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 59 | "..."); |
| 60 | |
| 61 | std::vector<std::string> display_order; |
| 62 | std::istringstream str(display_order_buf.data()); |
| 63 | for (std::string conn_name = ""; std::getline(str, conn_name, ',');) { |
| 64 | trim(conn_name); |
| 65 | display_order.push_back(std::move(conn_name)); |
| 66 | } |
| 67 | return display_order; |
| 68 | } |
| 69 | |
| 70 | static std::vector<DrmConnector *> make_primary_display_candidates( |
| 71 | std::vector<std::unique_ptr<DrmConnector>> &connectors) { |
| 72 | std::vector<DrmConnector *> primary_candidates; |
| 73 | std::transform(std::begin(connectors), std::end(connectors), |
| 74 | std::back_inserter(primary_candidates), |
| 75 | [](std::unique_ptr<DrmConnector> &conn) { |
| 76 | return conn.get(); |
| 77 | }); |
| 78 | primary_candidates.erase(std::remove_if(std::begin(primary_candidates), |
| 79 | std::end(primary_candidates), |
| 80 | [](const DrmConnector *conn) { |
| 81 | return conn->state() != |
| 82 | DRM_MODE_CONNECTED; |
| 83 | }), |
| 84 | std::end(primary_candidates)); |
| 85 | |
| 86 | std::vector<std::string> display_order = read_primary_display_order_prop(); |
| 87 | bool use_other = display_order.back() == "..."; |
| 88 | |
| 89 | // putting connectors from primary_display_order first |
| 90 | auto curr_connector = std::begin(primary_candidates); |
| 91 | for (const std::string &display_name : display_order) { |
| 92 | auto it = std::find_if(std::begin(primary_candidates), |
| 93 | std::end(primary_candidates), |
| 94 | [&display_name](const DrmConnector *conn) { |
| 95 | return conn->name() == display_name; |
| 96 | }); |
| 97 | if (it != std::end(primary_candidates)) { |
| 98 | std::iter_swap(it, curr_connector); |
| 99 | ++curr_connector; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (use_other) { |
| 104 | // then putting internal connectors second, everything else afterwards |
| 105 | std::partition(curr_connector, std::end(primary_candidates), |
| 106 | [](const DrmConnector *conn) { return conn->internal(); }); |
| 107 | } else { |
| 108 | primary_candidates.erase(curr_connector, std::end(primary_candidates)); |
| 109 | } |
| 110 | |
| 111 | return primary_candidates; |
| 112 | } |
| 113 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 114 | DrmDevice::DrmDevice() : event_listener_(this) { |
Sean Paul | 047b9b2 | 2015-07-28 14:15:42 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 117 | DrmDevice::~DrmDevice() { |
Sean Paul | 047b9b2 | 2015-07-28 14:15:42 -0400 | [diff] [blame] | 118 | event_listener_.Exit(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 121 | std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 122 | /* TODO: Use drmOpenControl here instead */ |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 123 | fd_.Set(open(path, O_RDWR)); |
| 124 | if (fd() < 0) { |
Peter Collingbourne | c77052e | 2020-02-19 11:25:08 -0800 | [diff] [blame] | 125 | ALOGE("Failed to open dri %s: %s", path, strerror(errno)); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 126 | return std::make_tuple(-ENODEV, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 129 | int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 130 | if (ret) { |
| 131 | ALOGE("Failed to set universal plane cap %d", ret); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 132 | return std::make_tuple(ret, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 135 | ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 136 | if (ret) { |
| 137 | ALOGE("Failed to set atomic cap %d", ret); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 138 | return std::make_tuple(ret, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Alexandru Gheorghe | b46b930 | 2018-03-21 14:19:58 +0000 | [diff] [blame] | 141 | #ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS |
| 142 | ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1); |
| 143 | if (ret) { |
| 144 | ALOGI("Failed to set writeback cap %d", ret); |
| 145 | ret = 0; |
| 146 | } |
| 147 | #endif |
| 148 | |
John Stultz | fa00233 | 2021-02-02 01:34:45 +0000 | [diff] [blame] | 149 | drmSetMaster(fd()); |
| 150 | if (!drmIsMaster(fd())) { |
| 151 | ALOGE("DRM/KMS master access required"); |
| 152 | return std::make_tuple(-EACCES, 0); |
Roman Stratiienko | 3b24cd9 | 2021-01-13 10:32:04 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 155 | drmModeResPtr res = drmModeGetResources(fd()); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 156 | if (!res) { |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 157 | ALOGE("Failed to get DrmDevice resources"); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 158 | return std::make_tuple(-ENODEV, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 161 | min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width, |
| 162 | res->min_height); |
| 163 | max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width, |
| 164 | res->max_height); |
Sean Paul | 406dbfc | 2016-02-10 15:35:17 -0800 | [diff] [blame] | 165 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 166 | // Assumes that the primary display will always be in the first |
| 167 | // drm_device opened. |
| 168 | bool found_primary = num_displays != 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 169 | |
| 170 | for (int i = 0; !ret && i < res->count_crtcs; ++i) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 171 | drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 172 | if (!c) { |
| 173 | ALOGE("Failed to get crtc %d", res->crtcs[i]); |
| 174 | ret = -ENODEV; |
| 175 | break; |
| 176 | } |
| 177 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 178 | std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 179 | drmModeFreeCrtc(c); |
| 180 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 181 | ret = crtc->Init(); |
| 182 | if (ret) { |
| 183 | ALOGE("Failed to initialize crtc %d", res->crtcs[i]); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 184 | break; |
| 185 | } |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 186 | crtcs_.emplace_back(std::move(crtc)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Alexandru Gheorghe | e8b668c | 2018-03-22 11:31:29 +0000 | [diff] [blame] | 189 | std::vector<int> possible_clones; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 190 | for (int i = 0; !ret && i < res->count_encoders; ++i) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 191 | drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 192 | if (!e) { |
| 193 | ALOGE("Failed to get encoder %d", res->encoders[i]); |
| 194 | ret = -ENODEV; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | std::vector<DrmCrtc *> possible_crtcs; |
| 199 | DrmCrtc *current_crtc = NULL; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 200 | for (auto &crtc : crtcs_) { |
| 201 | if ((1 << crtc->pipe()) & e->possible_crtcs) |
| 202 | possible_crtcs.push_back(crtc.get()); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 203 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 204 | if (crtc->id() == e->crtc_id) |
| 205 | current_crtc = crtc.get(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 208 | std::unique_ptr<DrmEncoder> enc( |
| 209 | new DrmEncoder(e, current_crtc, possible_crtcs)); |
Alexandru Gheorghe | e8b668c | 2018-03-22 11:31:29 +0000 | [diff] [blame] | 210 | possible_clones.push_back(e->possible_clones); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 211 | drmModeFreeEncoder(e); |
| 212 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 213 | encoders_.emplace_back(std::move(enc)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Alexandru Gheorghe | e8b668c | 2018-03-22 11:31:29 +0000 | [diff] [blame] | 216 | for (unsigned int i = 0; i < encoders_.size(); i++) { |
| 217 | for (unsigned int j = 0; j < encoders_.size(); j++) |
| 218 | if (possible_clones[i] & (1 << j)) |
| 219 | encoders_[i]->AddPossibleClone(encoders_[j].get()); |
| 220 | } |
| 221 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 222 | for (int i = 0; !ret && i < res->count_connectors; ++i) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 223 | drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 224 | if (!c) { |
| 225 | ALOGE("Failed to get connector %d", res->connectors[i]); |
| 226 | ret = -ENODEV; |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | std::vector<DrmEncoder *> possible_encoders; |
| 231 | DrmEncoder *current_encoder = NULL; |
| 232 | for (int j = 0; j < c->count_encoders; ++j) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 233 | for (auto &encoder : encoders_) { |
| 234 | if (encoder->id() == c->encoders[j]) |
| 235 | possible_encoders.push_back(encoder.get()); |
| 236 | if (encoder->id() == c->encoder_id) |
| 237 | current_encoder = encoder.get(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 241 | std::unique_ptr<DrmConnector> conn( |
| 242 | new DrmConnector(this, c, current_encoder, possible_encoders)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 243 | |
| 244 | drmModeFreeConnector(c); |
| 245 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 246 | ret = conn->Init(); |
| 247 | if (ret) { |
| 248 | ALOGE("Init connector %d failed", res->connectors[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 249 | break; |
| 250 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 251 | |
Alexandru Gheorghe | b46b930 | 2018-03-21 14:19:58 +0000 | [diff] [blame] | 252 | if (conn->writeback()) |
| 253 | writeback_connectors_.emplace_back(std::move(conn)); |
| 254 | else |
| 255 | connectors_.emplace_back(std::move(conn)); |
Robert Foss | 610d989 | 2017-11-01 12:50:04 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 258 | // Primary display priority: |
Jason Macnak | f1af957 | 2020-08-20 11:49:51 -0700 | [diff] [blame] | 259 | // 1) vendor.hwc.drm.primary_display_order property |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 260 | // 2) internal connectors |
| 261 | // 3) anything else |
| 262 | std::vector<DrmConnector *> |
| 263 | primary_candidates = make_primary_display_candidates(connectors_); |
| 264 | if (!primary_candidates.empty() && !found_primary) { |
| 265 | DrmConnector &conn = **std::begin(primary_candidates); |
| 266 | conn.set_display(num_displays); |
| 267 | displays_[num_displays] = num_displays; |
| 268 | ++num_displays; |
| 269 | found_primary = true; |
| 270 | } else { |
| 271 | ALOGE( |
Jason Macnak | f1af957 | 2020-08-20 11:49:51 -0700 | [diff] [blame] | 272 | "Failed to find primary display from " |
| 273 | "\"vendor.hwc.drm.primary_display_order\" property"); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 274 | } |
Robert Foss | 610d989 | 2017-11-01 12:50:04 -0500 | [diff] [blame] | 275 | |
Roman Kovalivskyi | d07c370 | 2019-11-04 17:54:31 +0200 | [diff] [blame] | 276 | // If no priority display were found then pick first available as primary and |
| 277 | // for the others assign consecutive display_numbers. |
Robert Foss | 610d989 | 2017-11-01 12:50:04 -0500 | [diff] [blame] | 278 | for (auto &conn : connectors_) { |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 279 | if (conn->external() || conn->internal()) { |
| 280 | if (!found_primary) { |
| 281 | conn->set_display(num_displays); |
| 282 | displays_[num_displays] = num_displays; |
| 283 | found_primary = true; |
| 284 | ++num_displays; |
| 285 | } else if (conn->display() < 0) { |
| 286 | conn->set_display(num_displays); |
| 287 | displays_[num_displays] = num_displays; |
| 288 | ++num_displays; |
| 289 | } |
Robert Foss | 610d989 | 2017-11-01 12:50:04 -0500 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 293 | if (res) |
| 294 | drmModeFreeResources(res); |
| 295 | |
| 296 | // Catch-all for the above loops |
| 297 | if (ret) |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 298 | return std::make_tuple(ret, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 299 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 300 | drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd()); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 301 | if (!plane_res) { |
| 302 | ALOGE("Failed to get plane resources"); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 303 | return std::make_tuple(-ENOENT, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | for (uint32_t i = 0; i < plane_res->count_planes; ++i) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 307 | drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 308 | if (!p) { |
| 309 | ALOGE("Failed to get plane %d", plane_res->planes[i]); |
| 310 | ret = -ENODEV; |
| 311 | break; |
| 312 | } |
| 313 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 314 | std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 315 | |
| 316 | drmModeFreePlane(p); |
| 317 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 318 | ret = plane->Init(); |
| 319 | if (ret) { |
| 320 | ALOGE("Init plane %d failed", plane_res->planes[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 324 | planes_.emplace_back(std::move(plane)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 325 | } |
| 326 | drmModeFreePlaneResources(plane_res); |
| 327 | if (ret) |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 328 | return std::make_tuple(ret, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 329 | |
Sean Paul | 047b9b2 | 2015-07-28 14:15:42 -0400 | [diff] [blame] | 330 | ret = event_listener_.Init(); |
| 331 | if (ret) { |
| 332 | ALOGE("Can't initialize event listener %d", ret); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 333 | return std::make_tuple(ret, 0); |
Sean Paul | 047b9b2 | 2015-07-28 14:15:42 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 336 | for (auto &conn : connectors_) { |
| 337 | ret = CreateDisplayPipe(conn.get()); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 338 | if (ret) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 339 | ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 340 | return std::make_tuple(ret, 0); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 341 | } |
Alexandru Gheorghe | b46b930 | 2018-03-21 14:19:58 +0000 | [diff] [blame] | 342 | if (!AttachWriteback(conn.get())) { |
| 343 | ALOGI("Display %d has writeback attach to it", conn->display()); |
| 344 | } |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 345 | } |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 346 | return std::make_tuple(ret, displays_.size()); |
| 347 | } |
| 348 | |
| 349 | bool DrmDevice::HandlesDisplay(int display) const { |
| 350 | return displays_.find(display) != displays_.end(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 353 | DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 354 | for (auto &conn : connectors_) { |
| 355 | if (conn->display() == display) |
| 356 | return conn.get(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 357 | } |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Alexandru Gheorghe | b46b930 | 2018-03-21 14:19:58 +0000 | [diff] [blame] | 361 | DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const { |
| 362 | for (auto &conn : writeback_connectors_) { |
| 363 | if (conn->display() == display) |
| 364 | return conn.get(); |
| 365 | } |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 369 | // TODO what happens when hotplugging |
| 370 | DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const { |
| 371 | DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display); |
| 372 | DrmConnector *display_conn = GetConnectorForDisplay(display); |
| 373 | // If we have a writeback already attached to the same CRTC just use that, |
| 374 | // if possible. |
| 375 | if (display_conn && writeback_conn && |
| 376 | writeback_conn->encoder()->CanClone(display_conn->encoder())) |
| 377 | return writeback_conn; |
| 378 | |
| 379 | // Use another CRTC if available and doesn't have any connector |
| 380 | for (auto &crtc : crtcs_) { |
| 381 | if (crtc->display() == display) |
| 382 | continue; |
| 383 | display_conn = GetConnectorForDisplay(crtc->display()); |
| 384 | // If we have a display connected don't use it for writeback |
| 385 | if (display_conn && display_conn->state() == DRM_MODE_CONNECTED) |
| 386 | continue; |
| 387 | writeback_conn = GetWritebackConnectorForDisplay(crtc->display()); |
| 388 | if (writeback_conn) |
| 389 | return writeback_conn; |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 394 | DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 395 | for (auto &crtc : crtcs_) { |
| 396 | if (crtc->display() == display) |
| 397 | return crtc.get(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 398 | } |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 402 | DrmPlane *DrmDevice::GetPlane(uint32_t id) const { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 403 | for (auto &plane : planes_) { |
| 404 | if (plane->id() == id) |
| 405 | return plane.get(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 406 | } |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 410 | const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const { |
Robert Foss | 0690c1c | 2016-10-20 11:07:57 -0400 | [diff] [blame] | 411 | return crtcs_; |
| 412 | } |
| 413 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 414 | uint32_t DrmDevice::next_mode_id() { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 415 | return ++mode_id_; |
| 416 | } |
| 417 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 418 | int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 419 | /* First try to use the currently-bound crtc */ |
| 420 | DrmCrtc *crtc = enc->crtc(); |
| 421 | if (crtc && crtc->can_bind(display)) { |
| 422 | crtc->set_display(display); |
Alexandru Gheorghe | ae4324c | 2018-03-21 12:06:20 +0000 | [diff] [blame] | 423 | enc->set_crtc(crtc); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* Try to find a possible crtc which will work */ |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 428 | for (DrmCrtc *crtc : enc->possible_crtcs()) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 429 | /* We've already tried this earlier */ |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 430 | if (crtc == enc->crtc()) |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 431 | continue; |
| 432 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 433 | if (crtc->can_bind(display)) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 434 | crtc->set_display(display); |
Alexandru Gheorghe | ae4324c | 2018-03-21 12:06:20 +0000 | [diff] [blame] | 435 | enc->set_crtc(crtc); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* We can't use the encoder, but nothing went wrong, try another one */ |
| 441 | return -EAGAIN; |
| 442 | } |
| 443 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 444 | int DrmDevice::CreateDisplayPipe(DrmConnector *connector) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 445 | int display = connector->display(); |
| 446 | /* Try to use current setup first */ |
| 447 | if (connector->encoder()) { |
| 448 | int ret = TryEncoderForDisplay(display, connector->encoder()); |
| 449 | if (!ret) { |
| 450 | return 0; |
| 451 | } else if (ret != -EAGAIN) { |
| 452 | ALOGE("Could not set mode %d/%d", display, ret); |
| 453 | return ret; |
| 454 | } |
| 455 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 456 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 457 | for (DrmEncoder *enc : connector->possible_encoders()) { |
| 458 | int ret = TryEncoderForDisplay(display, enc); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 459 | if (!ret) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 460 | connector->set_encoder(enc); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 461 | return 0; |
| 462 | } else if (ret != -EAGAIN) { |
| 463 | ALOGE("Could not set mode %d/%d", display, ret); |
| 464 | return ret; |
| 465 | } |
| 466 | } |
| 467 | ALOGE("Could not find a suitable encoder/crtc for display %d", |
| 468 | connector->display()); |
| 469 | return -ENODEV; |
| 470 | } |
| 471 | |
Alexandru Gheorghe | b46b930 | 2018-03-21 14:19:58 +0000 | [diff] [blame] | 472 | // Attach writeback connector to the CRTC linked to the display_conn |
| 473 | int DrmDevice::AttachWriteback(DrmConnector *display_conn) { |
| 474 | DrmCrtc *display_crtc = display_conn->encoder()->crtc(); |
| 475 | if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) { |
| 476 | ALOGE("Display already has writeback attach to it"); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | for (auto &writeback_conn : writeback_connectors_) { |
| 480 | if (writeback_conn->display() >= 0) |
| 481 | continue; |
| 482 | for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) { |
| 483 | for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) { |
| 484 | if (possible_crtc != display_crtc) |
| 485 | continue; |
| 486 | // Use just encoders which had not been bound already |
| 487 | if (writeback_enc->can_bind(display_crtc->display())) { |
| 488 | writeback_enc->set_crtc(display_crtc); |
| 489 | writeback_conn->set_encoder(writeback_enc); |
| 490 | writeback_conn->set_display(display_crtc->display()); |
| 491 | writeback_conn->UpdateModes(); |
| 492 | return 0; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 500 | int DrmDevice::CreatePropertyBlob(void *data, size_t length, |
| 501 | uint32_t *blob_id) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 502 | struct drm_mode_create_blob create_blob; |
| 503 | memset(&create_blob, 0, sizeof(create_blob)); |
| 504 | create_blob.length = length; |
| 505 | create_blob.data = (__u64)data; |
| 506 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 507 | int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 508 | if (ret) { |
| 509 | ALOGE("Failed to create mode property blob %d", ret); |
| 510 | return ret; |
| 511 | } |
| 512 | *blob_id = create_blob.blob_id; |
| 513 | return 0; |
| 514 | } |
| 515 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 516 | int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) { |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 517 | if (!blob_id) |
| 518 | return 0; |
| 519 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 520 | struct drm_mode_destroy_blob destroy_blob; |
| 521 | memset(&destroy_blob, 0, sizeof(destroy_blob)); |
| 522 | destroy_blob.blob_id = (__u32)blob_id; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 523 | int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 524 | if (ret) { |
Sean Paul | f741c67 | 2016-05-11 13:49:38 -0400 | [diff] [blame] | 525 | ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 526 | return ret; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 531 | DrmEventListener *DrmDevice::event_listener() { |
Sean Paul | 047b9b2 | 2015-07-28 14:15:42 -0400 | [diff] [blame] | 532 | return &event_listener_; |
| 533 | } |
| 534 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 535 | int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type, |
| 536 | const char *prop_name, DrmProperty *property) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 537 | drmModeObjectPropertiesPtr props; |
| 538 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 539 | props = drmModeObjectGetProperties(fd(), obj_id, obj_type); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 540 | if (!props) { |
| 541 | ALOGE("Failed to get properties for %d/%x", obj_id, obj_type); |
| 542 | return -ENODEV; |
| 543 | } |
| 544 | |
| 545 | bool found = false; |
| 546 | for (int i = 0; !found && (size_t)i < props->count_props; ++i) { |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 547 | drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 548 | if (!strcmp(p->name, prop_name)) { |
| 549 | property->Init(p, props->prop_values[i]); |
| 550 | found = true; |
| 551 | } |
| 552 | drmModeFreeProperty(p); |
| 553 | } |
| 554 | |
| 555 | drmModeFreeObjectProperties(props); |
| 556 | return found ? 0 : -ENOENT; |
| 557 | } |
| 558 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 559 | int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name, |
| 560 | DrmProperty *property) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 561 | return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property); |
| 562 | } |
| 563 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 564 | int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name, |
| 565 | DrmProperty *property) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 566 | return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property); |
| 567 | } |
| 568 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 569 | int DrmDevice::GetConnectorProperty(const DrmConnector &connector, |
| 570 | const char *prop_name, |
| 571 | DrmProperty *property) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 572 | return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name, |
| 573 | property); |
| 574 | } |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 575 | |
| 576 | const std::string DrmDevice::GetName() const { |
| 577 | auto ver = drmGetVersion(fd_.get()); |
| 578 | if (!ver) { |
| 579 | ALOGW("Failed to get drm version for fd=%d", fd_.get()); |
| 580 | return "generic"; |
| 581 | } |
| 582 | |
| 583 | std::string name(ver->name); |
| 584 | drmFreeVersion(ver); |
| 585 | return name; |
| 586 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 587 | } // namespace android |