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 ret = compositor_.Init();
246 if (ret)
247 return ret;
248
249 for (auto i = begin_connectors(); i != end_connectors(); ++i) {
250 ret = CreateDisplayPipe(*i);
251 if (ret) {
252 ALOGE("Failed CreateDisplayPipe %d with %d", (*i)->id(), ret);
253 return ret;
254 }
255 }
256 return 0;
257 }
258
fd() const259 int DrmResources::fd() const {
260 return fd_;
261 }
262
begin_connectors() const263 DrmResources::ConnectorIter DrmResources::begin_connectors() const {
264 return connectors_.begin();
265 }
266
end_connectors() const267 DrmResources::ConnectorIter DrmResources::end_connectors() const {
268 return connectors_.end();
269 }
270
GetConnectorForDisplay(int display) const271 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
272 for (ConnectorIter iter = connectors_.begin(); iter != connectors_.end();
273 ++iter) {
274 if ((*iter)->display() == display)
275 return *iter;
276 }
277 return NULL;
278 }
279
GetCrtcForDisplay(int display) const280 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
281 for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
282 iter != crtcs_.end(); ++iter) {
283 if ((*iter)->display() == display)
284 return *iter;
285 }
286 return NULL;
287 }
288
begin_planes() const289 DrmResources::PlaneIter DrmResources::begin_planes() const {
290 return planes_.begin();
291 }
292
end_planes() const293 DrmResources::PlaneIter DrmResources::end_planes() const {
294 return planes_.end();
295 }
296
GetPlane(uint32_t id) const297 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
298 for (std::vector<DrmPlane *>::const_iterator iter = planes_.begin();
299 iter != planes_.end(); ++iter) {
300 if ((*iter)->id() == id)
301 return *iter;
302 }
303 return NULL;
304 }
305
next_mode_id()306 uint32_t DrmResources::next_mode_id() {
307 return ++mode_id_;
308 }
309
TryEncoderForDisplay(int display,DrmEncoder * enc)310 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
311 /* First try to use the currently-bound crtc */
312 DrmCrtc *crtc = enc->crtc();
313 if (crtc && crtc->can_bind(display)) {
314 crtc->set_display(display);
315 return 0;
316 }
317
318 /* Try to find a possible crtc which will work */
319 for (DrmEncoder::CrtcIter iter = enc->begin_possible_crtcs();
320 iter != enc->end_possible_crtcs(); ++iter) {
321 /* We've already tried this earlier */
322 if (*iter == enc->crtc())
323 continue;
324
325 if ((*iter)->can_bind(display)) {
326 enc->set_crtc(*iter);
327 (*iter)->set_display(display);
328 return 0;
329 }
330 }
331
332 /* We can't use the encoder, but nothing went wrong, try another one */
333 return -EAGAIN;
334 }
335
CreateDisplayPipe(DrmConnector * connector)336 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
337 int display = connector->display();
338 /* Try to use current setup first */
339 if (connector->encoder()) {
340 int ret = TryEncoderForDisplay(display, connector->encoder());
341 if (!ret) {
342 return 0;
343 } else if (ret != -EAGAIN) {
344 ALOGE("Could not set mode %d/%d", display, ret);
345 return ret;
346 }
347 }
348
349 for (DrmConnector::EncoderIter iter = connector->begin_possible_encoders();
350 iter != connector->end_possible_encoders(); ++iter) {
351 int ret = TryEncoderForDisplay(display, *iter);
352 if (!ret) {
353 connector->set_encoder(*iter);
354 return 0;
355 } else if (ret != -EAGAIN) {
356 ALOGE("Could not set mode %d/%d", display, ret);
357 return ret;
358 }
359 }
360 ALOGE("Could not find a suitable encoder/crtc for display %d",
361 connector->display());
362 return -ENODEV;
363 }
364
CreatePropertyBlob(void * data,size_t length,uint32_t * blob_id)365 int DrmResources::CreatePropertyBlob(void *data, size_t length,
366 uint32_t *blob_id) {
367 struct drm_mode_create_blob create_blob;
368 memset(&create_blob, 0, sizeof(create_blob));
369 create_blob.length = length;
370 create_blob.data = (__u64)data;
371
372 int ret = drmIoctl(fd_, DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
373 if (ret) {
374 ALOGE("Failed to create mode property blob %d", ret);
375 return ret;
376 }
377 *blob_id = create_blob.blob_id;
378 return 0;
379 }
380
DestroyPropertyBlob(uint32_t blob_id)381 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
382 if (!blob_id)
383 return 0;
384
385 struct drm_mode_destroy_blob destroy_blob;
386 memset(&destroy_blob, 0, sizeof(destroy_blob));
387 destroy_blob.blob_id = (__u32)blob_id;
388 int ret = drmIoctl(fd_, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
389 if (ret) {
390 ALOGE("Failed to destroy mode property blob %ld/%d", blob_id, ret);
391 return ret;
392 }
393 return 0;
394 }
395
SetDisplayActiveMode(int display,const DrmMode & mode)396 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
397 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
398 if (!comp) {
399 ALOGE("Failed to create composition for dpms on %d", display);
400 return -ENOMEM;
401 }
402 int ret = comp->SetDisplayMode(display, mode);
403 if (ret) {
404 ALOGE("Failed to add mode to composition on %d %d", display, ret);
405 return ret;
406 }
407 ret = compositor_.QueueComposition(std::move(comp));
408 if (ret) {
409 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
410 return ret;
411 }
412 return 0;
413 }
414
SetDpmsMode(int display,uint64_t mode)415 int DrmResources::SetDpmsMode(int display, uint64_t mode) {
416 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
417 ALOGE("Invalid dpms mode %d", mode);
418 return -EINVAL;
419 }
420
421 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
422 if (!comp) {
423 ALOGE("Failed to create composition for dpms on %d", display);
424 return -ENOMEM;
425 }
426 int ret = comp->SetDpmsMode(display, mode);
427 if (ret) {
428 ALOGE("Failed to add dpms %ld to composition on %d %d", mode, display, ret);
429 return ret;
430 }
431 ret = compositor_.QueueComposition(std::move(comp));
432 if (ret) {
433 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
434 return ret;
435 }
436 return 0;
437 }
438
compositor()439 DrmCompositor *DrmResources::compositor() {
440 return &compositor_;
441 }
442
GetProperty(uint32_t obj_id,uint32_t obj_type,const char * prop_name,DrmProperty * property)443 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
444 const char *prop_name, DrmProperty *property) {
445 drmModeObjectPropertiesPtr props;
446
447 props = drmModeObjectGetProperties(fd_, obj_id, obj_type);
448 if (!props) {
449 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
450 return -ENODEV;
451 }
452
453 bool found = false;
454 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
455 drmModePropertyPtr p = drmModeGetProperty(fd_, props->props[i]);
456 if (!strcmp(p->name, prop_name)) {
457 property->Init(p, props->prop_values[i]);
458 found = true;
459 }
460 drmModeFreeProperty(p);
461 }
462
463 drmModeFreeObjectProperties(props);
464 return found ? 0 : -ENOENT;
465 }
466
GetPlaneProperty(const DrmPlane & plane,const char * prop_name,DrmProperty * property)467 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
468 DrmProperty *property) {
469 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
470 }
471
GetCrtcProperty(const DrmCrtc & crtc,const char * prop_name,DrmProperty * property)472 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
473 DrmProperty *property) {
474 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
475 }
476
GetConnectorProperty(const DrmConnector & connector,const char * prop_name,DrmProperty * property)477 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
478 const char *prop_name,
479 DrmProperty *property) {
480 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
481 property);
482 }
483 }
484