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"
30 #include <xf86drmMode.h>
32 #include <cutils/log.h>
33 #include <cutils/properties.h>
37 DrmResources::DrmResources() : compositor_(this), event_listener_(this) {
40 DrmResources::~DrmResources() {
41 event_listener_.Exit();
44 int DrmResources::Init() {
45 char path[PROPERTY_VALUE_MAX];
46 property_get("hwc.drm.device", path, "/dev/dri/card0");
48 /* TODO: Use drmOpenControl here instead */
49 fd_.Set(open(path, O_RDWR));
51 ALOGE("Failed to open dri- %s", strerror(-errno));
55 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
57 ALOGE("Failed to set universal plane cap %d", ret);
61 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
63 ALOGE("Failed to set atomic cap %d", ret);
67 drmModeResPtr res = drmModeGetResources(fd());
69 ALOGE("Failed to get DrmResources resources");
73 bool found_primary = false;
76 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
77 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
79 ALOGE("Failed to get crtc %d", res->crtcs[i]);
84 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
89 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
92 crtcs_.emplace_back(std::move(crtc));
95 for (int i = 0; !ret && i < res->count_encoders; ++i) {
96 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
98 ALOGE("Failed to get encoder %d", res->encoders[i]);
103 std::vector<DrmCrtc *> possible_crtcs;
104 DrmCrtc *current_crtc = NULL;
105 for (auto &crtc : crtcs_) {
106 if ((1 << crtc->pipe()) & e->possible_crtcs)
107 possible_crtcs.push_back(crtc.get());
109 if (crtc->id() == e->crtc_id)
110 current_crtc = crtc.get();
113 std::unique_ptr<DrmEncoder> enc(
114 new DrmEncoder(e, current_crtc, possible_crtcs));
116 drmModeFreeEncoder(e);
118 encoders_.emplace_back(std::move(enc));
121 for (int i = 0; !ret && i < res->count_connectors; ++i) {
122 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
124 ALOGE("Failed to get connector %d", res->connectors[i]);
129 std::vector<DrmEncoder *> possible_encoders;
130 DrmEncoder *current_encoder = NULL;
131 for (int j = 0; j < c->count_encoders; ++j) {
132 for (auto &encoder : encoders_) {
133 if (encoder->id() == c->encoders[j])
134 possible_encoders.push_back(encoder.get());
135 if (encoder->id() == c->encoder_id)
136 current_encoder = encoder.get();
140 std::unique_ptr<DrmConnector> conn(
141 new DrmConnector(this, c, current_encoder, possible_encoders));
143 drmModeFreeConnector(c);
147 ALOGE("Init connector %d failed", res->connectors[i]);
151 if (conn->built_in() && !found_primary) {
152 conn->set_display(0);
153 found_primary = true;
155 conn->set_display(display_num);
159 connectors_.emplace_back(std::move(conn));
162 drmModeFreeResources(res);
164 // Catch-all for the above loops
168 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
170 ALOGE("Failed to get plane resources");
174 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
175 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
177 ALOGE("Failed to get plane %d", plane_res->planes[i]);
182 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
188 ALOGE("Init plane %d failed", plane_res->planes[i]);
192 planes_.emplace_back(std::move(plane));
194 drmModeFreePlaneResources(plane_res);
198 ret = compositor_.Init();
202 ret = event_listener_.Init();
204 ALOGE("Can't initialize event listener %d", ret);
208 for (auto &conn : connectors_) {
209 ret = CreateDisplayPipe(conn.get());
211 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
218 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
219 for (auto &conn : connectors_) {
220 if (conn->display() == display)
226 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
227 for (auto &crtc : crtcs_) {
228 if (crtc->display() == display)
234 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
235 for (auto &plane : planes_) {
236 if (plane->id() == id)
242 uint32_t DrmResources::next_mode_id() {
246 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
247 /* First try to use the currently-bound crtc */
248 DrmCrtc *crtc = enc->crtc();
249 if (crtc && crtc->can_bind(display)) {
250 crtc->set_display(display);
254 /* Try to find a possible crtc which will work */
255 for (DrmCrtc *crtc : enc->possible_crtcs()) {
256 /* We've already tried this earlier */
257 if (crtc == enc->crtc())
260 if (crtc->can_bind(display)) {
262 crtc->set_display(display);
267 /* We can't use the encoder, but nothing went wrong, try another one */
271 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
272 int display = connector->display();
273 /* Try to use current setup first */
274 if (connector->encoder()) {
275 int ret = TryEncoderForDisplay(display, connector->encoder());
278 } else if (ret != -EAGAIN) {
279 ALOGE("Could not set mode %d/%d", display, ret);
284 for (DrmEncoder *enc : connector->possible_encoders()) {
285 int ret = TryEncoderForDisplay(display, enc);
287 connector->set_encoder(enc);
289 } else if (ret != -EAGAIN) {
290 ALOGE("Could not set mode %d/%d", display, ret);
294 ALOGE("Could not find a suitable encoder/crtc for display %d",
295 connector->display());
299 int DrmResources::CreatePropertyBlob(void *data, size_t length,
301 struct drm_mode_create_blob create_blob;
302 memset(&create_blob, 0, sizeof(create_blob));
303 create_blob.length = length;
304 create_blob.data = (__u64)data;
306 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
308 ALOGE("Failed to create mode property blob %d", ret);
311 *blob_id = create_blob.blob_id;
315 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
319 struct drm_mode_destroy_blob destroy_blob;
320 memset(&destroy_blob, 0, sizeof(destroy_blob));
321 destroy_blob.blob_id = (__u32)blob_id;
322 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
324 ALOGE("Failed to destroy mode property blob %ld/%d", blob_id, ret);
330 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
331 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
333 ALOGE("Failed to create composition for dpms on %d", display);
336 int ret = comp->SetDisplayMode(display, mode);
338 ALOGE("Failed to add mode to composition on %d %d", display, ret);
341 ret = compositor_.QueueComposition(std::move(comp));
343 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
349 int DrmResources::SetDpmsMode(int display, uint64_t mode) {
350 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
351 ALOGE("Invalid dpms mode %d", mode);
355 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
357 ALOGE("Failed to create composition for dpms on %d", display);
360 int ret = comp->SetDpmsMode(display, mode);
362 ALOGE("Failed to add dpms %ld to composition on %d %d", mode, display, ret);
365 ret = compositor_.QueueComposition(std::move(comp));
367 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
373 DrmCompositor *DrmResources::compositor() {
377 DrmEventListener *DrmResources::event_listener() {
378 return &event_listener_;
381 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
382 const char *prop_name, DrmProperty *property) {
383 drmModeObjectPropertiesPtr props;
385 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
387 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
392 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
393 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
394 if (!strcmp(p->name, prop_name)) {
395 property->Init(p, props->prop_values[i]);
398 drmModeFreeProperty(p);
401 drmModeFreeObjectProperties(props);
402 return found ? 0 : -ENOENT;
405 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
406 DrmProperty *property) {
407 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
410 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
411 DrmProperty *property) {
412 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
415 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
416 const char *prop_name,
417 DrmProperty *property) {
418 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,