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
17 #define LOG_TAG "hwc-drm-resources"
18
19 #include "drmconnector.h"
20 #include "drmcrtc.h"
21 #include "drmencoder.h"
22 #include "drmplane.h"
23 #include "drmresources.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <xf86drm.h>
29 #include <xf86drmMode.h>
30
31 #include <cutils/log.h>
32 #include <cutils/properties.h>
33
34 namespace android {
35
DrmResources()36 DrmResources::DrmResources() : fd_(-1), mode_id_(0), compositor_(this) {
37 }
38
~DrmResources()39 DrmResources::~DrmResources() {
40 for (std::vector<DrmConnector *>::const_iterator iter = connectors_.begin();
41 iter != connectors_.end(); ++iter)
42 delete *iter;
43 connectors_.clear();
44
45 for (std::vector<DrmEncoder *>::const_iterator iter = encoders_.begin();
46 iter != encoders_.end(); ++iter)
47 delete *iter;
48 encoders_.clear();
49
50 for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
51 iter != crtcs_.end(); ++iter)
52 delete *iter;
53 crtcs_.clear();
54
55 for (std::vector<DrmPlane *>::const_iterator iter = planes_.begin();
56 iter != planes_.end(); ++iter)
57 delete *iter;
58 planes_.clear();
59
60 if (fd_ >= 0)
61 close(fd_);
62 }
63
Init()64 int DrmResources::Init() {
65 char path[PROPERTY_VALUE_MAX];
66 property_get("hwc.drm.device", path, "/dev/dri/card0");
67
68 /* TODO: Use drmOpenControl here instead */
69 fd_ = open(path, O_RDWR);
70 if (fd_ < 0) {
71 ALOGE("Failed to open dri- %s", strerror(-errno));
72 return -ENODEV;
73 }
74
75 int ret = drmSetClientCap(fd_, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
76 if (ret) {
77 ALOGE("Failed to set universal plane cap %d", ret);
78 return ret;
79 }
80
81 ret = drmSetClientCap(fd_, DRM_CLIENT_CAP_ATOMIC, 1);
82 if (ret) {
83 ALOGE("Failed to set atomic cap %d", ret);
84 return ret;
85 }
86
87 drmModeResPtr res = drmModeGetResources(fd_);
88 if (!res) {
89 ALOGE("Failed to get DrmResources resources");
90 return -ENODEV;
91 }
92
93 bool found_primary = false;
94 int display_num = 1;
95
96 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
97 drmModeCrtcPtr c = drmModeGetCrtc(fd_, res->crtcs[i]);
98 if (!c) {
99 ALOGE("Failed to get crtc %d", res->crtcs[i]);
100 ret = -ENODEV;
101 break;
102 }
103
104 DrmCrtc *crtc = new DrmCrtc(this, c, i);
105
106 drmModeFreeCrtc(c);
107
108 if (!crtc) {
109 ALOGE("Failed to allocate crtc %d", res->crtcs[i]);
110 ret = -ENOMEM;
111 break;
112 }
113
114 ret = crtc->Init();
115 if (ret) {
116 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
117 delete crtc;
118 break;
119 }
120 crtcs_.push_back(crtc);
121 }
122
123 for (int i = 0; !ret && i < res->count_encoders; ++i) {
124 drmModeEncoderPtr e = drmModeGetEncoder(fd_, res->encoders[i]);
125 if (!e) {
126 ALOGE("Failed to get encoder %d", res->encoders[i]);
127 ret = -ENODEV;
128 break;
129 }
130
131 std::vector<DrmCrtc *> possible_crtcs;
132 DrmCrtc *current_crtc = NULL;
133 for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
134 iter != crtcs_.end(); ++iter) {
135 if ((1 << (*iter)->pipe()) & e->possible_crtcs)
136 possible_crtcs.push_back(*iter);
137
138 if ((*iter)->id() == e->crtc_id)
139 current_crtc = (*iter);
140 }
141
142 DrmEncoder *enc = new DrmEncoder(e, current_crtc, possible_crtcs);
143
144 drmModeFreeEncoder(e);
145
146 if (!enc) {
147 ALOGE("Failed to allocate enc %d", res->encoders[i]);
148 ret = -ENOMEM;
149 break;
150 }
151 encoders_.push_back(enc);
152 }
153
154 for (int i = 0; !ret && i < res->count_connectors; ++i) {
155 drmModeConnectorPtr c = drmModeGetConnector(fd_, res->connectors[i]);
156 if (!c) {
157 ALOGE("Failed to get connector %d", res->connectors[i]);
158 ret = -ENODEV;
159 break;
160 }
161
162 std::vector<DrmEncoder *> possible_encoders;
163 DrmEncoder *current_encoder = NULL;
164 for (int j = 0; j < c->count_encoders; ++j) {
165 for (std::vector<DrmEncoder *>::const_iterator iter = encoders_.begin();
166 iter != encoders_.end(); ++iter) {
167 if ((*iter)->id() == c->encoders[j])
168 possible_encoders.push_back((*iter));
169 if ((*iter)->id() == c->encoder_id)
170 current_encoder = *iter;
171 }
172 }
173
174 DrmConnector *conn =
175 new DrmConnector(this, c, current_encoder, possible_encoders);
176
177 drmModeFreeConnector(c);
178
179 if (!conn) {
180 ALOGE("Failed to allocate conn %d", res->connectors[i]);
181 ret = -ENOMEM;
182 break;
183 }
184
185 ret = conn->Init();
186 if (ret) {
187 ALOGE("Init connector %d failed", res->connectors[i]);
188 delete conn;
189 break;
190 }
191 connectors_.push_back(conn);
192
193 if (conn->built_in() && !found_primary) {
194 conn->set_display(0);
195 found_primary = true;
196 } else {
197 conn->set_display(display_num);
198 ++display_num;
199 }
200 }
201 if (res)
202 drmModeFreeResources(res);
203
204 // Catch-all for the above loops
205 if (ret)
206 return ret;
207
208 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd_);
209 if (!plane_res) {
210 ALOGE("Failed to get plane resources");
211 return -ENOENT;
212 }
213
214 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
215 drmModePlanePtr p = drmModeGetPlane(fd_, plane_res->planes[i]);
216 if (!p) {
217 ALOGE("Failed to get plane %d", plane_res->planes[i]);
218 ret = -ENODEV;
219 break;
220 }
221
222 DrmPlane *plane = new DrmPlane(this, p);
223
224 drmModeFreePlane(p);
225
226 if (!plane) {
227 ALOGE("Allocate plane %d failed", plane_res->planes[i]);
228 ret = -ENOMEM;
229 break;
230 }
231
232 ret = plane->Init();
233 if (ret) {
234 ALOGE("Init plane %d failed", plane_res->planes[i]);
235 delete plane;
236 break;
237 }
238
239 planes_.push_back(plane);
240 }
241 drmModeFreePlaneResources(plane_res);
242 if (ret)
243 return ret;
244
245 return compositor_.Init();
246 }
247
fd() const248 int DrmResources::fd() const {
249 return fd_;
250 }
251
begin_connectors() const252 DrmResources::ConnectorIter DrmResources::begin_connectors() const {
253 return connectors_.begin();
254 }
255
end_connectors() const256 DrmResources::ConnectorIter DrmResources::end_connectors() const {
257 return connectors_.end();
258 }
259
GetConnectorForDisplay(int display) const260 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
261 for (ConnectorIter iter = connectors_.begin(); iter != connectors_.end();
262 ++iter) {
263 if ((*iter)->display() == display)
264 return *iter;
265 }
266 return NULL;
267 }
268
GetCrtcForDisplay(int display) const269 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
270 for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
271 iter != crtcs_.end(); ++iter) {
272 if ((*iter)->display() == display)
273 return *iter;
274 }
275 return NULL;
276 }
277
begin_planes() const278 DrmResources::PlaneIter DrmResources::begin_planes() const {
279 return planes_.begin();
280 }
281
end_planes() const282 DrmResources::PlaneIter DrmResources::end_planes() const {
283 return planes_.end();
284 }
285
GetPlane(uint32_t id) const286 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
287 for (std::vector<DrmPlane *>::const_iterator iter = planes_.begin();
288 iter != planes_.end(); ++iter) {
289 if ((*iter)->id() == id)
290 return *iter;
291 }
292 return NULL;
293 }
294
next_mode_id()295 uint32_t DrmResources::next_mode_id() {
296 return ++mode_id_;
297 }
298
TryEncoderForDisplay(int display,DrmEncoder * enc)299 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
300 /* First try to use the currently-bound crtc */
301 DrmCrtc *crtc = enc->crtc();
302 if (crtc && crtc->can_bind(display)) {
303 crtc->set_display(display);
304 return 0;
305 }
306
307 /* Try to find a possible crtc which will work */
308 for (DrmEncoder::CrtcIter iter = enc->begin_possible_crtcs();
309 iter != enc->end_possible_crtcs(); ++iter) {
310 /* We've already tried this earlier */
311 if (*iter == enc->crtc())
312 continue;
313
314 if ((*iter)->can_bind(display)) {
315 enc->set_crtc(*iter);
316 (*iter)->set_display(display);
317 return 0;
318 }
319 }
320
321 /* We can't use the encoder, but nothing went wrong, try another one */
322 return -EAGAIN;
323 }
324
CreateDisplayPipe(DrmConnector * connector)325 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
326 int display = connector->display();
327 /* Try to use current setup first */
328 if (connector->encoder()) {
329 int ret = TryEncoderForDisplay(display, connector->encoder());
330 if (!ret) {
331 return 0;
332 } else if (ret != -EAGAIN) {
333 ALOGE("Could not set mode %d/%d", display, ret);
334 return ret;
335 }
336 }
337
338 for (DrmConnector::EncoderIter iter = connector->begin_possible_encoders();
339 iter != connector->end_possible_encoders(); ++iter) {
340 int ret = TryEncoderForDisplay(display, *iter);
341 if (!ret) {
342 connector->set_encoder(*iter);
343 return 0;
344 } else if (ret != -EAGAIN) {
345 ALOGE("Could not set mode %d/%d", display, ret);
346 return ret;
347 }
348 }
349 ALOGE("Could not find a suitable encoder/crtc for display %d",
350 connector->display());
351 return -ENODEV;
352 }
353
CreatePropertyBlob(void * data,size_t length,uint32_t * blob_id)354 int DrmResources::CreatePropertyBlob(void *data, size_t length,
355 uint32_t *blob_id) {
356 struct drm_mode_create_blob create_blob;
357 memset(&create_blob, 0, sizeof(create_blob));
358 create_blob.length = length;
359 create_blob.data = (__u64)data;
360
361 int ret = drmIoctl(fd_, DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
362 if (ret) {
363 ALOGE("Failed to create mode property blob %d", ret);
364 return ret;
365 }
366 *blob_id = create_blob.blob_id;
367 return 0;
368 }
369
DestroyPropertyBlob(uint32_t blob_id)370 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
371 struct drm_mode_destroy_blob destroy_blob;
372 memset(&destroy_blob, 0, sizeof(destroy_blob));
373 destroy_blob.blob_id = (__u32)blob_id;
374 int ret = drmIoctl(fd_, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
375 if (ret) {
376 ALOGE("Failed to destroy mode property blob %d", ret);
377 return ret;
378 }
379 return 0;
380 }
381
SetDisplayActiveMode(int display,const DrmMode & mode)382 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
383 DrmConnector *connector = GetConnectorForDisplay(display);
384 if (!connector) {
385 ALOGE("Could not locate connector for display %d", display);
386 return -ENODEV;
387 }
388
389 int ret = CreateDisplayPipe(connector);
390 if (ret) {
391 ALOGE("Failed CreateDisplayPipe with %d", ret);
392 return ret;
393 }
394
395 DrmCrtc *crtc = connector->encoder()->crtc();
396 DrmProperty old_mode;
397 ret = GetCrtcProperty(*crtc, crtc->mode_property().name().c_str(), &old_mode);
398 if (ret) {
399 ALOGE("Failed to get old mode property from crtc %d", crtc->id());
400 return ret;
401 }
402
403 struct drm_mode_modeinfo drm_mode;
404 memset(&drm_mode, 0, sizeof(drm_mode));
405 mode.ToDrmModeModeInfo(&drm_mode);
406
407 uint32_t blob_id;
408 ret =
409 CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo), &blob_id);
410 if (ret) {
411 ALOGE("Failed to create mode property blob %d", ret);
412 return ret;
413 }
414
415 drmModePropertySetPtr pset = drmModePropertySetAlloc();
416 if (!pset) {
417 ALOGE("Failed to allocate property set");
418 DestroyPropertyBlob(blob_id);
419 return -ENOMEM;
420 }
421
422 ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
423 blob_id) ||
424 drmModePropertySetAdd(pset, connector->id(),
425 connector->crtc_id_property().id(), crtc->id());
426 if (ret) {
427 ALOGE("Failed to add blob %d to pset", blob_id);
428 DestroyPropertyBlob(blob_id);
429 drmModePropertySetFree(pset);
430 return ret;
431 }
432
433 ret =
434 drmModePropertySetCommit(fd_, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL, pset);
435
436 drmModePropertySetFree(pset);
437
438 if (ret) {
439 ALOGE("Failed to commit pset ret=%d\n", ret);
440 DestroyPropertyBlob(blob_id);
441 return ret;
442 }
443
444 connector->set_active_mode(mode);
445
446 uint64_t old_blob_id;
447 ret = old_mode.value(&old_blob_id);
448 if (ret) {
449 ALOGE("Could not get old blob id value %d", ret);
450 return ret;
451 }
452 if (!old_blob_id)
453 return 0;
454
455 ret = DestroyPropertyBlob(old_blob_id);
456 if (ret) {
457 ALOGE("Failed to destroy old mode property blob", old_blob_id);
458 return ret;
459 }
460
461 return 0;
462 }
463
SetDpmsMode(int display,uint64_t mode)464 int DrmResources::SetDpmsMode(int display, uint64_t mode) {
465 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
466 ALOGE("Invalid dpms mode %d", mode);
467 return -EINVAL;
468 }
469
470 DrmComposition *comp = (DrmComposition *)compositor_.CreateComposition(NULL);
471 if (!comp) {
472 ALOGE("Failed to create composition for dpms on %d", display);
473 return -ENOMEM;
474 }
475 int ret = comp->AddDpmsMode(display, mode);
476 if (ret) {
477 ALOGE("Failed to add dpms %ld to composition on %d %d", mode, display, ret);
478 delete comp;
479 return ret;
480 }
481 ret = compositor_.QueueComposition((Composition *)comp);
482 if (ret) {
483 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
484 return ret;
485 }
486 return 0;
487 }
488
compositor()489 DrmCompositor *DrmResources::compositor() {
490 return &compositor_;
491 }
492
GetProperty(uint32_t obj_id,uint32_t obj_type,const char * prop_name,DrmProperty * property)493 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
494 const char *prop_name, DrmProperty *property) {
495 drmModeObjectPropertiesPtr props;
496
497 props = drmModeObjectGetProperties(fd_, obj_id, obj_type);
498 if (!props) {
499 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
500 return -ENODEV;
501 }
502
503 bool found = false;
504 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
505 drmModePropertyPtr p = drmModeGetProperty(fd_, props->props[i]);
506 if (!strcmp(p->name, prop_name)) {
507 property->Init(p, props->prop_values[i]);
508 found = true;
509 }
510 drmModeFreeProperty(p);
511 }
512
513 drmModeFreeObjectProperties(props);
514 return found ? 0 : -ENOENT;
515 }
516
GetPlaneProperty(const DrmPlane & plane,const char * prop_name,DrmProperty * property)517 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
518 DrmProperty *property) {
519 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
520 }
521
GetCrtcProperty(const DrmCrtc & crtc,const char * prop_name,DrmProperty * property)522 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
523 DrmProperty *property) {
524 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
525 }
526
GetConnectorProperty(const DrmConnector & connector,const char * prop_name,DrmProperty * property)527 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
528 const char *prop_name,
529 DrmProperty *property) {
530 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
531 property);
532 }
533 }
534