• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "drmeventlistener.h"
23 #include "drmplane.h"
24 #include "drmresources.h"
25 
26 #include <cinttypes>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdint.h>
30 #include <xf86drm.h>
31 #include <xf86drmMode.h>
32 
33 #include <cutils/log.h>
34 #include <cutils/properties.h>
35 
36 namespace android {
37 
DrmResources()38 DrmResources::DrmResources() : compositor_(this), event_listener_(this) {
39 }
40 
~DrmResources()41 DrmResources::~DrmResources() {
42   event_listener_.Exit();
43 }
44 
Init()45 int DrmResources::Init() {
46   char path[PROPERTY_VALUE_MAX];
47   property_get("hwc.drm.device", path, "/dev/dri/card0");
48 
49   /* TODO: Use drmOpenControl here instead */
50   fd_.Set(open(path, O_RDWR));
51   if (fd() < 0) {
52     ALOGE("Failed to open dri- %s", strerror(-errno));
53     return -ENODEV;
54   }
55 
56   int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
57   if (ret) {
58     ALOGE("Failed to set universal plane cap %d", ret);
59     return ret;
60   }
61 
62   ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
63   if (ret) {
64     ALOGE("Failed to set atomic cap %d", ret);
65     return ret;
66   }
67 
68   drmModeResPtr res = drmModeGetResources(fd());
69   if (!res) {
70     ALOGE("Failed to get DrmResources resources");
71     return -ENODEV;
72   }
73 
74   bool found_primary = false;
75   int display_num = 1;
76 
77   for (int i = 0; !ret && i < res->count_crtcs; ++i) {
78     drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
79     if (!c) {
80       ALOGE("Failed to get crtc %d", res->crtcs[i]);
81       ret = -ENODEV;
82       break;
83     }
84 
85     std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
86     drmModeFreeCrtc(c);
87 
88     ret = crtc->Init();
89     if (ret) {
90       ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
91       break;
92     }
93     crtcs_.emplace_back(std::move(crtc));
94   }
95 
96   for (int i = 0; !ret && i < res->count_encoders; ++i) {
97     drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
98     if (!e) {
99       ALOGE("Failed to get encoder %d", res->encoders[i]);
100       ret = -ENODEV;
101       break;
102     }
103 
104     std::vector<DrmCrtc *> possible_crtcs;
105     DrmCrtc *current_crtc = NULL;
106     for (auto &crtc : crtcs_) {
107       if ((1 << crtc->pipe()) & e->possible_crtcs)
108         possible_crtcs.push_back(crtc.get());
109 
110       if (crtc->id() == e->crtc_id)
111         current_crtc = crtc.get();
112     }
113 
114     std::unique_ptr<DrmEncoder> enc(
115         new DrmEncoder(e, current_crtc, possible_crtcs));
116 
117     drmModeFreeEncoder(e);
118 
119     encoders_.emplace_back(std::move(enc));
120   }
121 
122   for (int i = 0; !ret && i < res->count_connectors; ++i) {
123     drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
124     if (!c) {
125       ALOGE("Failed to get connector %d", res->connectors[i]);
126       ret = -ENODEV;
127       break;
128     }
129 
130     std::vector<DrmEncoder *> possible_encoders;
131     DrmEncoder *current_encoder = NULL;
132     for (int j = 0; j < c->count_encoders; ++j) {
133       for (auto &encoder : encoders_) {
134         if (encoder->id() == c->encoders[j])
135           possible_encoders.push_back(encoder.get());
136         if (encoder->id() == c->encoder_id)
137           current_encoder = encoder.get();
138       }
139     }
140 
141     std::unique_ptr<DrmConnector> conn(
142         new DrmConnector(this, c, current_encoder, possible_encoders));
143 
144     drmModeFreeConnector(c);
145 
146     ret = conn->Init();
147     if (ret) {
148       ALOGE("Init connector %d failed", res->connectors[i]);
149       break;
150     }
151 
152     if (conn->built_in() && !found_primary) {
153       conn->set_display(0);
154       found_primary = true;
155     } else {
156       conn->set_display(display_num);
157       ++display_num;
158     }
159 
160     connectors_.emplace_back(std::move(conn));
161   }
162   if (res)
163     drmModeFreeResources(res);
164 
165   // Catch-all for the above loops
166   if (ret)
167     return ret;
168 
169   drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
170   if (!plane_res) {
171     ALOGE("Failed to get plane resources");
172     return -ENOENT;
173   }
174 
175   for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
176     drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
177     if (!p) {
178       ALOGE("Failed to get plane %d", plane_res->planes[i]);
179       ret = -ENODEV;
180       break;
181     }
182 
183     std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
184 
185     drmModeFreePlane(p);
186 
187     ret = plane->Init();
188     if (ret) {
189       ALOGE("Init plane %d failed", plane_res->planes[i]);
190       break;
191     }
192 
193     planes_.emplace_back(std::move(plane));
194   }
195   drmModeFreePlaneResources(plane_res);
196   if (ret)
197     return ret;
198 
199   ret = compositor_.Init();
200   if (ret)
201     return ret;
202 
203   ret = event_listener_.Init();
204   if (ret) {
205     ALOGE("Can't initialize event listener %d", ret);
206     return ret;
207   }
208 
209   for (auto &conn : connectors_) {
210     ret = CreateDisplayPipe(conn.get());
211     if (ret) {
212       ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
213       return ret;
214     }
215   }
216   return 0;
217 }
218 
GetConnectorForDisplay(int display) const219 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
220   for (auto &conn : connectors_) {
221     if (conn->display() == display)
222       return conn.get();
223   }
224   return NULL;
225 }
226 
GetCrtcForDisplay(int display) const227 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
228   for (auto &crtc : crtcs_) {
229     if (crtc->display() == display)
230       return crtc.get();
231   }
232   return NULL;
233 }
234 
GetPlane(uint32_t id) const235 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
236   for (auto &plane : planes_) {
237     if (plane->id() == id)
238       return plane.get();
239   }
240   return NULL;
241 }
242 
next_mode_id()243 uint32_t DrmResources::next_mode_id() {
244   return ++mode_id_;
245 }
246 
TryEncoderForDisplay(int display,DrmEncoder * enc)247 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
248   /* First try to use the currently-bound crtc */
249   DrmCrtc *crtc = enc->crtc();
250   if (crtc && crtc->can_bind(display)) {
251     crtc->set_display(display);
252     return 0;
253   }
254 
255   /* Try to find a possible crtc which will work */
256   for (DrmCrtc *crtc : enc->possible_crtcs()) {
257     /* We've already tried this earlier */
258     if (crtc == enc->crtc())
259       continue;
260 
261     if (crtc->can_bind(display)) {
262       enc->set_crtc(crtc);
263       crtc->set_display(display);
264       return 0;
265     }
266   }
267 
268   /* We can't use the encoder, but nothing went wrong, try another one */
269   return -EAGAIN;
270 }
271 
CreateDisplayPipe(DrmConnector * connector)272 int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
273   int display = connector->display();
274   /* Try to use current setup first */
275   if (connector->encoder()) {
276     int ret = TryEncoderForDisplay(display, connector->encoder());
277     if (!ret) {
278       return 0;
279     } else if (ret != -EAGAIN) {
280       ALOGE("Could not set mode %d/%d", display, ret);
281       return ret;
282     }
283   }
284 
285   for (DrmEncoder *enc : connector->possible_encoders()) {
286     int ret = TryEncoderForDisplay(display, enc);
287     if (!ret) {
288       connector->set_encoder(enc);
289       return 0;
290     } else if (ret != -EAGAIN) {
291       ALOGE("Could not set mode %d/%d", display, ret);
292       return ret;
293     }
294   }
295   ALOGE("Could not find a suitable encoder/crtc for display %d",
296         connector->display());
297   return -ENODEV;
298 }
299 
CreatePropertyBlob(void * data,size_t length,uint32_t * blob_id)300 int DrmResources::CreatePropertyBlob(void *data, size_t length,
301                                      uint32_t *blob_id) {
302   struct drm_mode_create_blob create_blob;
303   memset(&create_blob, 0, sizeof(create_blob));
304   create_blob.length = length;
305   create_blob.data = (__u64)data;
306 
307   int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
308   if (ret) {
309     ALOGE("Failed to create mode property blob %d", ret);
310     return ret;
311   }
312   *blob_id = create_blob.blob_id;
313   return 0;
314 }
315 
DestroyPropertyBlob(uint32_t blob_id)316 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
317   if (!blob_id)
318     return 0;
319 
320   struct drm_mode_destroy_blob destroy_blob;
321   memset(&destroy_blob, 0, sizeof(destroy_blob));
322   destroy_blob.blob_id = (__u32)blob_id;
323   int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
324   if (ret) {
325     ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
326     return ret;
327   }
328   return 0;
329 }
330 
SetDisplayActiveMode(int display,const DrmMode & mode)331 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) {
332   std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
333   if (!comp) {
334     ALOGE("Failed to create composition for dpms on %d", display);
335     return -ENOMEM;
336   }
337   int ret = comp->SetDisplayMode(display, mode);
338   if (ret) {
339     ALOGE("Failed to add mode to composition on %d %d", display, ret);
340     return ret;
341   }
342   ret = compositor_.QueueComposition(std::move(comp));
343   if (ret) {
344     ALOGE("Failed to queue dpms composition on %d %d", display, ret);
345     return ret;
346   }
347   return 0;
348 }
349 
SetDpmsMode(int display,uint64_t mode)350 int DrmResources::SetDpmsMode(int display, uint64_t mode) {
351   if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) {
352     ALOGE("Invalid dpms mode %" PRIu64, mode);
353     return -EINVAL;
354   }
355 
356   std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL));
357   if (!comp) {
358     ALOGE("Failed to create composition for dpms on %d", display);
359     return -ENOMEM;
360   }
361   int ret = comp->SetDpmsMode(display, mode);
362   if (ret) {
363     ALOGE("Failed to add dpms %" PRIu64 " to composition on %d %d", mode,
364           display, ret);
365     return ret;
366   }
367   ret = compositor_.QueueComposition(std::move(comp));
368   if (ret) {
369     ALOGE("Failed to queue dpms composition on %d %d", display, ret);
370     return ret;
371   }
372   return 0;
373 }
374 
compositor()375 DrmCompositor *DrmResources::compositor() {
376   return &compositor_;
377 }
378 
event_listener()379 DrmEventListener *DrmResources::event_listener() {
380   return &event_listener_;
381 }
382 
GetProperty(uint32_t obj_id,uint32_t obj_type,const char * prop_name,DrmProperty * property)383 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
384                               const char *prop_name, DrmProperty *property) {
385   drmModeObjectPropertiesPtr props;
386 
387   props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
388   if (!props) {
389     ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
390     return -ENODEV;
391   }
392 
393   bool found = false;
394   for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
395     drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
396     if (!strcmp(p->name, prop_name)) {
397       property->Init(p, props->prop_values[i]);
398       found = true;
399     }
400     drmModeFreeProperty(p);
401   }
402 
403   drmModeFreeObjectProperties(props);
404   return found ? 0 : -ENOENT;
405 }
406 
GetPlaneProperty(const DrmPlane & plane,const char * prop_name,DrmProperty * property)407 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
408                                    DrmProperty *property) {
409   return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
410 }
411 
GetCrtcProperty(const DrmCrtc & crtc,const char * prop_name,DrmProperty * property)412 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
413                                   DrmProperty *property) {
414   return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
415 }
416 
GetConnectorProperty(const DrmConnector & connector,const char * prop_name,DrmProperty * property)417 int DrmResources::GetConnectorProperty(const DrmConnector &connector,
418                                        const char *prop_name,
419                                        DrmProperty *property) {
420   return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
421                      property);
422 }
423 }
424