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"
23 #include "drmresources.h"
29 #include <xf86drmMode.h>
31 #include <cutils/log.h>
32 #include <cutils/properties.h>
36 DrmResources::DrmResources() : compositor_(this) {
39 int DrmResources::Init() {
40 char path[PROPERTY_VALUE_MAX];
41 property_get("hwc.drm.device", path, "/dev/dri/card0");
43 /* TODO: Use drmOpenControl here instead */
44 fd_.Set(open(path, O_RDWR));
46 ALOGE("Failed to open dri- %s", strerror(-errno));
50 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
52 ALOGE("Failed to set universal plane cap %d", ret);
56 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
58 ALOGE("Failed to set atomic cap %d", ret);
62 drmModeResPtr res = drmModeGetResources(fd());
64 ALOGE("Failed to get DrmResources resources");
68 bool found_primary = false;
71 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
72 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
74 ALOGE("Failed to get crtc %d", res->crtcs[i]);
79 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
84 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
87 crtcs_.emplace_back(std::move(crtc));
90 for (int i = 0; !ret && i < res->count_encoders; ++i) {
91 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
93 ALOGE("Failed to get encoder %d", res->encoders[i]);
98 std::vector<DrmCrtc *> possible_crtcs;
99 DrmCrtc *current_crtc = NULL;
100 for (auto &crtc : crtcs_) {
101 if ((1 << crtc->pipe()) & e->possible_crtcs)
102 possible_crtcs.push_back(crtc.get());
104 if (crtc->id() == e->crtc_id)
105 current_crtc = crtc.get();
108 std::unique_ptr<DrmEncoder> enc(
109 new DrmEncoder(e, current_crtc, possible_crtcs));
111 drmModeFreeEncoder(e);
113 encoders_.emplace_back(std::move(enc));
116 for (int i = 0; !ret && i < res->count_connectors; ++i) {
117 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
119 ALOGE("Failed to get connector %d", res->connectors[i]);
124 std::vector<DrmEncoder *> possible_encoders;
125 DrmEncoder *current_encoder = NULL;
126 for (int j = 0; j < c->count_encoders; ++j) {
127 for (auto &encoder : encoders_) {
128 if (encoder->id() == c->encoders[j])
129 possible_encoders.push_back(encoder.get());
130 if (encoder->id() == c->encoder_id)
131 current_encoder = encoder.get();
135 std::unique_ptr<DrmConnector> conn(
136 new DrmConnector(this, c, current_encoder, possible_encoders));
138 drmModeFreeConnector(c);
142 ALOGE("Init connector %d failed", res->connectors[i]);
146 if (conn->built_in() && !found_primary) {
147 conn->set_display(0);
148 found_primary = true;
150 conn->set_display(display_num);
154 connectors_.emplace_back(std::move(conn));
157 drmModeFreeResources(res);
159 // Catch-all for the above loops
163 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
165 ALOGE("Failed to get plane resources");
169 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
170 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
172 ALOGE("Failed to get plane %d", plane_res->planes[i]);
177 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
183 ALOGE("Init plane %d failed", plane_res->planes[i]);
187 planes_.emplace_back(std::move(plane));
189 drmModeFreePlaneResources(plane_res);
193 ret = compositor_.Init();
197 for (auto &conn : connectors_) {
198 ret = CreateDisplayPipe(conn.get());
200 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
207 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
208 for (auto &conn : connectors_) {
209 if (conn->display() == display)
215 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
216 for (auto &crtc : crtcs_) {
217 if (crtc->display() == display)
223 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
224 for (auto &plane : planes_) {
225 if (plane->id() == id)
231 uint32_t DrmResources::next_mode_id() {
235 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
236 /* First try to use the currently-bound crtc */
237 DrmCrtc *crtc = enc->crtc();
238 if (crtc && crtc->can_bind(display)) {
239 crtc->set_display(display);
243 /* Try to find a possible crtc which will work */
244 for (DrmCrtc *crtc : enc->possible_crtcs()) {
245 /* We've already tried this earlier */
246 if (crtc == enc->crtc())
249 if (crtc->can_bind(display)) {
251 crtc->set_display(display);
256 /* We can't use the encoder, but nothing went wrong, try another one */
260 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
261 int display = connector->display();
262 /* Try to use current setup first */
263 if (connector->encoder()) {
264 int ret = TryEncoderForDisplay(display, connector->encoder());
267 } else if (ret != -EAGAIN) {
268 ALOGE("Could not set mode %d/%d", display, ret);
273 for (DrmEncoder *enc : connector->possible_encoders()) {
274 int ret = TryEncoderForDisplay(display, enc);
276 connector->set_encoder(enc);
278 } else if (ret != -EAGAIN) {
279 ALOGE("Could not set mode %d/%d", display, ret);
283 ALOGE("Could not find a suitable encoder/crtc for display %d",
284 connector->display());
288 int DrmResources::CreatePropertyBlob(void *data, size_t length,
290 struct drm_mode_create_blob create_blob;
291 memset(&create_blob, 0, sizeof(create_blob));
292 create_blob.length = length;
293 create_blob.data = (__u64)data;
295 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
297 ALOGE("Failed to create mode property blob %d", ret);
300 *blob_id = create_blob.blob_id;
304 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
308 struct drm_mode_destroy_blob destroy_blob;
309 memset(&destroy_blob, 0, sizeof(destroy_blob));
310 destroy_blob.blob_id = (__u32)blob_id;
311 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
313 ALOGE("Failed to destroy mode property blob %ld/%d", blob_id, ret);
319 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
320 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
322 ALOGE("Failed to create composition for dpms on %d", display);
325 int ret = comp->SetDisplayMode(display, mode);
327 ALOGE("Failed to add mode to composition on %d %d", display, ret);
330 ret = compositor_.QueueComposition(std::move(comp));
332 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
338 int DrmResources::SetDpmsMode(int display, uint64_t mode) {
339 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
340 ALOGE("Invalid dpms mode %d", mode);
344 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
346 ALOGE("Failed to create composition for dpms on %d", display);
349 int ret = comp->SetDpmsMode(display, mode);
351 ALOGE("Failed to add dpms %ld to composition on %d %d", mode, display, ret);
354 ret = compositor_.QueueComposition(std::move(comp));
356 ALOGE("Failed to queue dpms composition on %d %d", display, ret);
362 DrmCompositor *DrmResources::compositor() {
366 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
367 const char *prop_name, DrmProperty *property) {
368 drmModeObjectPropertiesPtr props;
370 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
372 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
377 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
378 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
379 if (!strcmp(p->name, prop_name)) {
380 property->Init(p, props->prop_values[i]);
383 drmModeFreeProperty(p);
386 drmModeFreeObjectProperties(props);
387 return found ? 0 : -ENOENT;
390 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
391 DrmProperty *property) {
392 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
395 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
396 DrmProperty *property) {
397 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
400 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
401 const char *prop_name,
402 DrmProperty *property) {
403 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,