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