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