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