OSDN Git Service

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