2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-drm-resources"
19 #include "drmconnector.h"
21 #include "drmencoder.h"
22 #include "drmeventlistener.h"
24 #include "drmresources.h"
31 #include <xf86drmMode.h>
34 #include <cutils/properties.h>
38 DrmResources::DrmResources() : event_listener_(this) {
41 DrmResources::~DrmResources() {
42 event_listener_.Exit();
45 int DrmResources::Init() {
46 char path[PROPERTY_VALUE_MAX];
47 property_get("hwc.drm.device", path, "/dev/dri/card0");
49 /* TODO: Use drmOpenControl here instead */
50 fd_.Set(open(path, O_RDWR));
52 ALOGE("Failed to open dri- %s", strerror(-errno));
56 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
58 ALOGE("Failed to set universal plane cap %d", ret);
62 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
64 ALOGE("Failed to set atomic cap %d", ret);
68 drmModeResPtr res = drmModeGetResources(fd());
70 ALOGE("Failed to get DrmResources resources");
75 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height);
77 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height);
79 bool found_primary = false;
82 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
83 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
85 ALOGE("Failed to get crtc %d", res->crtcs[i]);
90 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
95 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
98 crtcs_.emplace_back(std::move(crtc));
101 for (int i = 0; !ret && i < res->count_encoders; ++i) {
102 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
104 ALOGE("Failed to get encoder %d", res->encoders[i]);
109 std::vector<DrmCrtc *> possible_crtcs;
110 DrmCrtc *current_crtc = NULL;
111 for (auto &crtc : crtcs_) {
112 if ((1 << crtc->pipe()) & e->possible_crtcs)
113 possible_crtcs.push_back(crtc.get());
115 if (crtc->id() == e->crtc_id)
116 current_crtc = crtc.get();
119 std::unique_ptr<DrmEncoder> enc(
120 new DrmEncoder(e, current_crtc, possible_crtcs));
122 drmModeFreeEncoder(e);
124 encoders_.emplace_back(std::move(enc));
127 for (int i = 0; !ret && i < res->count_connectors; ++i) {
128 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
130 ALOGE("Failed to get connector %d", res->connectors[i]);
135 std::vector<DrmEncoder *> possible_encoders;
136 DrmEncoder *current_encoder = NULL;
137 for (int j = 0; j < c->count_encoders; ++j) {
138 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();
146 std::unique_ptr<DrmConnector> conn(
147 new DrmConnector(this, c, current_encoder, possible_encoders));
149 drmModeFreeConnector(c);
153 ALOGE("Init connector %d failed", res->connectors[i]);
157 connectors_.emplace_back(std::move(conn));
160 // First look for primary amongst internal connectors
161 for (auto &conn : connectors_) {
162 if (conn->internal() && !found_primary) {
163 conn->set_display(0);
164 found_primary = true;
166 conn->set_display(display_num);
171 // Then look for primary amongst external connectors
172 for (auto &conn : connectors_) {
173 if (conn->external() && !found_primary) {
174 conn->set_display(0);
175 found_primary = true;
180 drmModeFreeResources(res);
182 // Catch-all for the above loops
186 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
188 ALOGE("Failed to get plane resources");
192 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
193 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
195 ALOGE("Failed to get plane %d", plane_res->planes[i]);
200 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
206 ALOGE("Init plane %d failed", plane_res->planes[i]);
210 planes_.emplace_back(std::move(plane));
212 drmModeFreePlaneResources(plane_res);
216 ret = event_listener_.Init();
218 ALOGE("Can't initialize event listener %d", ret);
222 for (auto &conn : connectors_) {
223 ret = CreateDisplayPipe(conn.get());
225 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
232 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
233 for (auto &conn : connectors_) {
234 if (conn->display() == display)
240 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
241 for (auto &crtc : crtcs_) {
242 if (crtc->display() == display)
248 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
249 for (auto &plane : planes_) {
250 if (plane->id() == id)
256 const std::vector<std::unique_ptr<DrmCrtc>> & DrmResources::crtcs() const {
260 uint32_t DrmResources::next_mode_id() {
264 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
265 /* First try to use the currently-bound crtc */
266 DrmCrtc *crtc = enc->crtc();
267 if (crtc && crtc->can_bind(display)) {
268 crtc->set_display(display);
272 /* Try to find a possible crtc which will work */
273 for (DrmCrtc *crtc : enc->possible_crtcs()) {
274 /* We've already tried this earlier */
275 if (crtc == enc->crtc())
278 if (crtc->can_bind(display)) {
280 crtc->set_display(display);
285 /* We can't use the encoder, but nothing went wrong, try another one */
289 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
290 int display = connector->display();
291 /* Try to use current setup first */
292 if (connector->encoder()) {
293 int ret = TryEncoderForDisplay(display, connector->encoder());
296 } else if (ret != -EAGAIN) {
297 ALOGE("Could not set mode %d/%d", display, ret);
302 for (DrmEncoder *enc : connector->possible_encoders()) {
303 int ret = TryEncoderForDisplay(display, enc);
305 connector->set_encoder(enc);
307 } else if (ret != -EAGAIN) {
308 ALOGE("Could not set mode %d/%d", display, ret);
312 ALOGE("Could not find a suitable encoder/crtc for display %d",
313 connector->display());
317 int DrmResources::CreatePropertyBlob(void *data, size_t length,
319 struct drm_mode_create_blob create_blob;
320 memset(&create_blob, 0, sizeof(create_blob));
321 create_blob.length = length;
322 create_blob.data = (__u64)data;
324 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
326 ALOGE("Failed to create mode property blob %d", ret);
329 *blob_id = create_blob.blob_id;
333 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
337 struct drm_mode_destroy_blob destroy_blob;
338 memset(&destroy_blob, 0, sizeof(destroy_blob));
339 destroy_blob.blob_id = (__u32)blob_id;
340 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
342 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
348 DrmEventListener *DrmResources::event_listener() {
349 return &event_listener_;
352 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
353 const char *prop_name, DrmProperty *property) {
354 drmModeObjectPropertiesPtr props;
356 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
358 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
363 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
364 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
365 if (!strcmp(p->name, prop_name)) {
366 property->Init(p, props->prop_values[i]);
369 drmModeFreeProperty(p);
372 drmModeFreeObjectProperties(props);
373 return found ? 0 : -ENOENT;
376 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
377 DrmProperty *property) {
378 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
381 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
382 DrmProperty *property) {
383 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
386 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
387 const char *prop_name,
388 DrmProperty *property) {
389 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,