blob: ea23cce0108f10967859e7fe23f4e0877bbc9e24 [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
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
17#define LOG_TAG "hwc-drm-resources"
18
19#include "drmconnector.h"
20#include "drmcrtc.h"
21#include "drmencoder.h"
Sean Paul047b9b22015-07-28 14:15:42 -040022#include "drmeventlistener.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040023#include "drmplane.h"
24#include "drmresources.h"
25
Sean Paulf741c672016-05-11 13:49:38 -040026#include <cinttypes>
Sean Paul6a55e9f2015-04-30 15:31:06 -040027#include <errno.h>
28#include <fcntl.h>
29#include <stdint.h>
30#include <xf86drm.h>
31#include <xf86drmMode.h>
32
33#include <cutils/log.h>
34#include <cutils/properties.h>
35
36namespace android {
37
Sean Paul047b9b22015-07-28 14:15:42 -040038DrmResources::DrmResources() : compositor_(this), event_listener_(this) {
39}
40
41DrmResources::~DrmResources() {
42 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -040043}
44
45int DrmResources::Init() {
46 char path[PROPERTY_VALUE_MAX];
47 property_get("hwc.drm.device", path, "/dev/dri/card0");
48
49 /* TODO: Use drmOpenControl here instead */
Zach Reiznerff30b522015-10-28 19:08:45 -070050 fd_.Set(open(path, O_RDWR));
51 if (fd() < 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040052 ALOGE("Failed to open dri- %s", strerror(-errno));
53 return -ENODEV;
54 }
55
Zach Reiznerff30b522015-10-28 19:08:45 -070056 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 if (ret) {
58 ALOGE("Failed to set universal plane cap %d", ret);
59 return ret;
60 }
61
Zach Reiznerff30b522015-10-28 19:08:45 -070062 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 if (ret) {
64 ALOGE("Failed to set atomic cap %d", ret);
65 return ret;
66 }
67
Zach Reiznerff30b522015-10-28 19:08:45 -070068 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040069 if (!res) {
70 ALOGE("Failed to get DrmResources resources");
71 return -ENODEV;
72 }
73
Sean Paul406dbfc2016-02-10 15:35:17 -080074 min_resolution_ =
75 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height);
76 max_resolution_ =
77 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height);
78
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 bool found_primary = false;
80 int display_num = 1;
81
82 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -070083 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040084 if (!c) {
85 ALOGE("Failed to get crtc %d", res->crtcs[i]);
86 ret = -ENODEV;
87 break;
88 }
89
Zach Reiznerff30b522015-10-28 19:08:45 -070090 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 drmModeFreeCrtc(c);
92
Sean Paul877be972015-06-03 14:08:27 -040093 ret = crtc->Init();
94 if (ret) {
95 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -040096 break;
97 }
Zach Reiznerff30b522015-10-28 19:08:45 -070098 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -040099 }
100
101 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700102 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400103 if (!e) {
104 ALOGE("Failed to get encoder %d", res->encoders[i]);
105 ret = -ENODEV;
106 break;
107 }
108
109 std::vector<DrmCrtc *> possible_crtcs;
110 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700111 for (auto &crtc : crtcs_) {
112 if ((1 << crtc->pipe()) & e->possible_crtcs)
113 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114
Zach Reiznerff30b522015-10-28 19:08:45 -0700115 if (crtc->id() == e->crtc_id)
116 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400117 }
118
Zach Reiznerff30b522015-10-28 19:08:45 -0700119 std::unique_ptr<DrmEncoder> enc(
120 new DrmEncoder(e, current_crtc, possible_crtcs));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121
122 drmModeFreeEncoder(e);
123
Zach Reiznerff30b522015-10-28 19:08:45 -0700124 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400125 }
126
127 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700128 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 if (!c) {
130 ALOGE("Failed to get connector %d", res->connectors[i]);
131 ret = -ENODEV;
132 break;
133 }
134
135 std::vector<DrmEncoder *> possible_encoders;
136 DrmEncoder *current_encoder = NULL;
137 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700138 for (auto &encoder : encoders_) {
139 if (encoder->id() == c->encoders[j])
140 possible_encoders.push_back(encoder.get());
141 if (encoder->id() == c->encoder_id)
142 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400143 }
144 }
145
Zach Reiznerff30b522015-10-28 19:08:45 -0700146 std::unique_ptr<DrmConnector> conn(
147 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148
149 drmModeFreeConnector(c);
150
Sean Paul6a55e9f2015-04-30 15:31:06 -0400151 ret = conn->Init();
152 if (ret) {
153 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154 break;
155 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400156
157 if (conn->built_in() && !found_primary) {
158 conn->set_display(0);
159 found_primary = true;
160 } else {
161 conn->set_display(display_num);
162 ++display_num;
163 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700164
165 connectors_.emplace_back(std::move(conn));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400166 }
167 if (res)
168 drmModeFreeResources(res);
169
170 // Catch-all for the above loops
171 if (ret)
172 return ret;
173
Zach Reiznerff30b522015-10-28 19:08:45 -0700174 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400175 if (!plane_res) {
176 ALOGE("Failed to get plane resources");
177 return -ENOENT;
178 }
179
180 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700181 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400182 if (!p) {
183 ALOGE("Failed to get plane %d", plane_res->planes[i]);
184 ret = -ENODEV;
185 break;
186 }
187
Zach Reiznerff30b522015-10-28 19:08:45 -0700188 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400189
190 drmModeFreePlane(p);
191
Sean Paul6a55e9f2015-04-30 15:31:06 -0400192 ret = plane->Init();
193 if (ret) {
194 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400195 break;
196 }
197
Zach Reiznerff30b522015-10-28 19:08:45 -0700198 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400199 }
200 drmModeFreePlaneResources(plane_res);
201 if (ret)
202 return ret;
203
Sean Paul57355412015-09-19 09:14:34 -0400204 ret = compositor_.Init();
205 if (ret)
206 return ret;
207
Sean Paul047b9b22015-07-28 14:15:42 -0400208 ret = event_listener_.Init();
209 if (ret) {
210 ALOGE("Can't initialize event listener %d", ret);
211 return ret;
212 }
213
Zach Reiznerff30b522015-10-28 19:08:45 -0700214 for (auto &conn : connectors_) {
215 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400216 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700217 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Sean Paul57355412015-09-19 09:14:34 -0400218 return ret;
219 }
220 }
221 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400222}
223
Sean Paul6a55e9f2015-04-30 15:31:06 -0400224DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700225 for (auto &conn : connectors_) {
226 if (conn->display() == display)
227 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400228 }
229 return NULL;
230}
231
232DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700233 for (auto &crtc : crtcs_) {
234 if (crtc->display() == display)
235 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236 }
237 return NULL;
238}
239
Sean Paul6a55e9f2015-04-30 15:31:06 -0400240DrmPlane *DrmResources::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700241 for (auto &plane : planes_) {
242 if (plane->id() == id)
243 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244 }
245 return NULL;
246}
247
Robert Foss0690c1c2016-10-20 11:07:57 -0400248const std::vector<std::unique_ptr<DrmCrtc>> & DrmResources::crtcs() const {
249 return crtcs_;
250}
251
Sean Paul6a55e9f2015-04-30 15:31:06 -0400252uint32_t DrmResources::next_mode_id() {
253 return ++mode_id_;
254}
255
256int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
257 /* First try to use the currently-bound crtc */
258 DrmCrtc *crtc = enc->crtc();
259 if (crtc && crtc->can_bind(display)) {
260 crtc->set_display(display);
261 return 0;
262 }
263
264 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700265 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400266 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700267 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400268 continue;
269
Zach Reiznerff30b522015-10-28 19:08:45 -0700270 if (crtc->can_bind(display)) {
271 enc->set_crtc(crtc);
272 crtc->set_display(display);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400273 return 0;
274 }
275 }
276
277 /* We can't use the encoder, but nothing went wrong, try another one */
278 return -EAGAIN;
279}
280
Sean Paul877be972015-06-03 14:08:27 -0400281int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
282 int display = connector->display();
283 /* Try to use current setup first */
284 if (connector->encoder()) {
285 int ret = TryEncoderForDisplay(display, connector->encoder());
286 if (!ret) {
287 return 0;
288 } else if (ret != -EAGAIN) {
289 ALOGE("Could not set mode %d/%d", display, ret);
290 return ret;
291 }
292 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400293
Zach Reiznerff30b522015-10-28 19:08:45 -0700294 for (DrmEncoder *enc : connector->possible_encoders()) {
295 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400296 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700297 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400298 return 0;
299 } else if (ret != -EAGAIN) {
300 ALOGE("Could not set mode %d/%d", display, ret);
301 return ret;
302 }
303 }
304 ALOGE("Could not find a suitable encoder/crtc for display %d",
305 connector->display());
306 return -ENODEV;
307}
308
309int DrmResources::CreatePropertyBlob(void *data, size_t length,
310 uint32_t *blob_id) {
311 struct drm_mode_create_blob create_blob;
312 memset(&create_blob, 0, sizeof(create_blob));
313 create_blob.length = length;
314 create_blob.data = (__u64)data;
315
Zach Reiznerff30b522015-10-28 19:08:45 -0700316 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400317 if (ret) {
318 ALOGE("Failed to create mode property blob %d", ret);
319 return ret;
320 }
321 *blob_id = create_blob.blob_id;
322 return 0;
323}
324
325int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400326 if (!blob_id)
327 return 0;
328
Sean Paul877be972015-06-03 14:08:27 -0400329 struct drm_mode_destroy_blob destroy_blob;
330 memset(&destroy_blob, 0, sizeof(destroy_blob));
331 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700332 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400333 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400334 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400335 return ret;
336 }
337 return 0;
338}
339
340int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
Sean Paul57355412015-09-19 09:14:34 -0400341 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
342 if (!comp) {
343 ALOGE("Failed to create composition for dpms on %d", display);
Sean Paul877be972015-06-03 14:08:27 -0400344 return -ENOMEM;
345 }
Sean Paul57355412015-09-19 09:14:34 -0400346 int ret = comp->SetDisplayMode(display, mode);
Sean Paul877be972015-06-03 14:08:27 -0400347 if (ret) {
Sean Paul57355412015-09-19 09:14:34 -0400348 ALOGE("Failed to add mode to composition on %d %d", display, ret);
Sean Paul877be972015-06-03 14:08:27 -0400349 return ret;
350 }
Sean Paul57355412015-09-19 09:14:34 -0400351 ret = compositor_.QueueComposition(std::move(comp));
Sean Paul877be972015-06-03 14:08:27 -0400352 if (ret) {
Sean Paul57355412015-09-19 09:14:34 -0400353 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
Sean Paul877be972015-06-03 14:08:27 -0400354 return ret;
355 }
Sean Paul877be972015-06-03 14:08:27 -0400356 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400357}
358
359int DrmResources::SetDpmsMode(int display, uint64_t mode) {
360 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
Sean Paulf741c672016-05-11 13:49:38 -0400361 ALOGE("Invalid dpms mode %" PRIu64, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400362 return -EINVAL;
363 }
364
Zach Reizner09807052015-08-13 14:53:41 -0700365 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
Sean Pauldb7a17d2015-06-24 18:46:05 -0700366 if (!comp) {
367 ALOGE("Failed to create composition for dpms on %d", display);
368 return -ENOMEM;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400369 }
Zach Reizner09807052015-08-13 14:53:41 -0700370 int ret = comp->SetDpmsMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400371 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400372 ALOGE("Failed to add dpms %" PRIu64 " to composition on %d %d", mode,
373 display, ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400374 return ret;
375 }
Zach Reizner09807052015-08-13 14:53:41 -0700376 ret = compositor_.QueueComposition(std::move(comp));
Sean Pauldb7a17d2015-06-24 18:46:05 -0700377 if (ret) {
378 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
379 return ret;
380 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400381 return 0;
382}
383
Sean Paulb386f1b2015-05-13 06:33:23 -0700384DrmCompositor *DrmResources::compositor() {
385 return &compositor_;
386}
387
Sean Paul047b9b22015-07-28 14:15:42 -0400388DrmEventListener *DrmResources::event_listener() {
389 return &event_listener_;
390}
391
Sean Paul6a55e9f2015-04-30 15:31:06 -0400392int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
393 const char *prop_name, DrmProperty *property) {
394 drmModeObjectPropertiesPtr props;
395
Zach Reiznerff30b522015-10-28 19:08:45 -0700396 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400397 if (!props) {
398 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
399 return -ENODEV;
400 }
401
402 bool found = false;
403 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700404 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400405 if (!strcmp(p->name, prop_name)) {
406 property->Init(p, props->prop_values[i]);
407 found = true;
408 }
409 drmModeFreeProperty(p);
410 }
411
412 drmModeFreeObjectProperties(props);
413 return found ? 0 : -ENOENT;
414}
415
416int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
417 DrmProperty *property) {
418 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
419}
420
Sean Paul877be972015-06-03 14:08:27 -0400421int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
422 DrmProperty *property) {
423 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
424}
425
Sean Paul6a55e9f2015-04-30 15:31:06 -0400426int DrmResources::GetConnectorProperty(const DrmConnector &connector,
427 const char *prop_name,
428 DrmProperty *property) {
429 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
430 property);
431}
432}