OSDN Git Service

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