OSDN Git Service

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