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