blob: 912bb74f93236bb885a2118181ce55d3e91e15a5 [file] [log] [blame]
Sean Paule0c4c3d2015-01-20 16:56:04 -05001/*
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 "hwcomposer-drm"
18
19#include <fcntl.h>
20#include <errno.h>
21#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050022#include <sys/resource.h>
23#include <pthread.h>
Sean Paul9b1bb842015-01-23 01:11:58 -050024#include <queue>
Sean Paule0c4c3d2015-01-20 16:56:04 -050025
26#include <cutils/log.h>
27
28#include <xf86drm.h>
29#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050030
31#include <hardware/hardware.h>
32#include <hardware/hwcomposer.h>
33
Sean Paul9aa5ad32015-01-22 15:47:54 -050034#include <sync/sync.h>
Sean Paulf1dc1912015-01-24 01:34:31 -050035#include <sw_sync.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050036
Sean Paulcd36a9e2015-01-22 18:01:18 -050037#include "drm_hwcomposer.h"
Sean Paule0c4c3d2015-01-20 16:56:04 -050038
39#define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0]))
40
41#define HWCOMPOSER_DRM_DEVICE "/dev/dri/card0"
42#define MAX_NUM_DISPLAYS 3
43#define UM_PER_INCH 25400
44
45static const uint32_t panel_types[] = {
46 DRM_MODE_CONNECTOR_LVDS,
47 DRM_MODE_CONNECTOR_eDP,
48 DRM_MODE_CONNECTOR_DSI,
49};
50
Sean Paul9aa5ad32015-01-22 15:47:54 -050051struct hwc_worker {
52 pthread_t thread;
53 pthread_mutex_t lock;
54 pthread_cond_t cond;
55 bool exit;
56};
57
Sean Paule0c4c3d2015-01-20 16:56:04 -050058struct hwc_drm_display {
Sean Paul9aa5ad32015-01-22 15:47:54 -050059 struct hwc_context_t *ctx;
60 int display;
61
Sean Paule0c4c3d2015-01-20 16:56:04 -050062 uint32_t connector_id;
63
64 drmModeModeInfoPtr configs;
65 uint32_t num_configs;
66
Sean Paulfa406a12015-02-04 10:05:44 -080067 drmModeModeInfo active_mode;
Sean Paule0c4c3d2015-01-20 16:56:04 -050068 uint32_t active_crtc;
Sean Pauleb9e75c2015-01-25 23:31:30 -050069 int active_pipe;
Sean Paulefb20cb2015-02-04 09:29:15 -080070 bool initial_modeset_required;
Sean Paul9aa5ad32015-01-22 15:47:54 -050071
72 struct hwc_worker set_worker;
73
Sean Paul9b1bb842015-01-23 01:11:58 -050074 std::queue<struct hwc_drm_bo> buf_queue;
Sean Paul9aa5ad32015-01-22 15:47:54 -050075 struct hwc_drm_bo front;
Sean Paulf1dc1912015-01-24 01:34:31 -050076
77 int timeline_fd;
78 unsigned timeline_next;
Sean Pauleb9e75c2015-01-25 23:31:30 -050079
80 struct hwc_worker vsync_worker;
81 bool enable_vsync_events;
Sean Paule0c4c3d2015-01-20 16:56:04 -050082};
83
84struct hwc_context_t {
85 hwc_composer_device_1_t device;
86
87 int fd;
Sean Paule0c4c3d2015-01-20 16:56:04 -050088
89 hwc_procs_t const *procs;
Sean Paulcd36a9e2015-01-22 18:01:18 -050090 struct hwc_import_context *import_ctx;
Sean Paule0c4c3d2015-01-20 16:56:04 -050091
92 struct hwc_drm_display displays[MAX_NUM_DISPLAYS];
93 int num_displays;
94};
95
96static int hwc_get_drm_display(struct hwc_context_t *ctx, int display,
97 struct hwc_drm_display **hd)
98{
99 if (display >= MAX_NUM_DISPLAYS) {
100 ALOGE("Requested display is out-of-bounds %d %d", display,
101 MAX_NUM_DISPLAYS);
102 return -EINVAL;
103 }
104 *hd = &ctx->displays[display];
105 return 0;
106}
107
108static int hwc_prepare_layer(hwc_layer_1_t *layer)
109{
110 /* TODO: We can't handle background right now, defer to sufaceFlinger */
111 if (layer->compositionType == HWC_BACKGROUND) {
112 layer->compositionType = HWC_FRAMEBUFFER;
113 ALOGV("Can't handle background layers yet");
114
115 /* TODO: Support sideband compositions */
116 } else if (layer->compositionType == HWC_SIDEBAND) {
117 layer->compositionType = HWC_FRAMEBUFFER;
118 ALOGV("Can't handle sideband content yet");
119 }
120
121 layer->hints = 0;
122
123 /* TODO: Handle cursor by setting compositionType=HWC_CURSOR_OVERLAY */
124 if (layer->flags & HWC_IS_CURSOR_LAYER) {
125 ALOGV("Can't handle async cursors yet");
126 }
127
128 /* TODO: Handle transformations */
129 if (layer->transform) {
130 ALOGV("Can't handle transformations yet");
131 }
132
133 /* TODO: Handle blending & plane alpha*/
134 if (layer->blending == HWC_BLENDING_PREMULT ||
135 layer->blending == HWC_BLENDING_COVERAGE) {
136 ALOGV("Can't handle blending yet");
137 }
138
139 /* TODO: Handle cropping & scaling */
140
141 return 0;
142}
143
144static int hwc_prepare(hwc_composer_device_1_t */* dev */, size_t num_displays,
145 hwc_display_contents_1_t** display_contents)
146{
147 int ret = 0, i, j;
148
149 /* TODO: Check flags for HWC_GEOMETRY_CHANGED */
150
151 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
Sean Pauldffca952015-02-04 10:19:55 -0800152
153 if (!display_contents[i])
154 continue;
155
Sean Paule0c4c3d2015-01-20 16:56:04 -0500156 for (j = 0; j < (int)display_contents[i]->numHwLayers; j++) {
157 ret = hwc_prepare_layer(
158 &display_contents[i]->hwLayers[j]);
159 if (ret) {
160 ALOGE("Failed to prepare layer %d:%d", j, i);
161 return ret;
162 }
163 }
164 }
165
166 return ret;
167}
168
Sean Paule0c4c3d2015-01-20 16:56:04 -0500169static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b)
170{
171 return a->clock == b->clock &&
172 a->hdisplay == b->hdisplay &&
173 a->hsync_start == b->hsync_start &&
174 a->hsync_end == b->hsync_end &&
175 a->htotal == b->htotal &&
176 a->hskew == b->hskew &&
177 a->vdisplay == b->vdisplay &&
178 a->vsync_start == b->vsync_start &&
179 a->vsync_end == b->vsync_end &&
180 a->vtotal == b->vtotal &&
181 a->vscan == b->vscan &&
182 a->vrefresh == b->vrefresh &&
183 a->flags == b->flags &&
184 a->type == b->type &&
185 !strcmp(a->name, b->name);
186}
187
Sean Paul9aa5ad32015-01-22 15:47:54 -0500188static int hwc_modeset_required(struct hwc_drm_display *hd,
189 bool *modeset_required)
190{
191 drmModeCrtcPtr crtc;
192 drmModeModeInfoPtr m;
193
Sean Paulefb20cb2015-02-04 09:29:15 -0800194 if (hd->initial_modeset_required) {
195 *modeset_required = true;
196 hd->initial_modeset_required = false;
197 return 0;
198 }
199
Sean Paul9aa5ad32015-01-22 15:47:54 -0500200 crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc);
201 if (!crtc) {
202 ALOGE("Failed to get crtc for display %d", hd->display);
203 return -ENODEV;
204 }
205
Sean Paulfa406a12015-02-04 10:05:44 -0800206 m = &hd->active_mode;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500207
208 /* Do a modeset if we haven't done one, or the mode has changed */
209 if (!crtc->mode_valid || !hwc_mode_is_equal(m, &crtc->mode))
210 *modeset_required = true;
211 else
212 *modeset_required = false;
213
214 drmModeFreeCrtc(crtc);
215
216 return 0;
217}
218
219static void hwc_flip_handler(int /* fd */, unsigned int /* sequence */,
220 unsigned int /* tv_sec */, unsigned int /* tv_usec */,
221 void */* user_data */)
222{
223}
224
Sean Paul9b1bb842015-01-23 01:11:58 -0500225static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500226{
227 fd_set fds;
228 drmEventContext event_context;
229 int ret;
230 bool modeset_required;
231
232 ret = hwc_modeset_required(hd, &modeset_required);
233 if (ret) {
234 ALOGE("Failed to determine if modeset is required %d", ret);
235 return ret;
236 }
237 if (modeset_required) {
Sean Paul9b1bb842015-01-23 01:11:58 -0500238 ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id,
239 0, 0, &hd->connector_id, 1,
Sean Paulfa406a12015-02-04 10:05:44 -0800240 &hd->active_mode);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500241 if (ret) {
242 ALOGE("Modeset failed for crtc %d",
243 hd->active_crtc);
244 return ret;
245 }
246 return 0;
247 }
248
249 FD_ZERO(&fds);
250 FD_SET(hd->ctx->fd, &fds);
251
252 event_context.version = DRM_EVENT_CONTEXT_VERSION;
253 event_context.page_flip_handler = hwc_flip_handler;
254
Sean Paul9b1bb842015-01-23 01:11:58 -0500255 ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id,
Sean Paul9aa5ad32015-01-22 15:47:54 -0500256 DRM_MODE_PAGE_FLIP_EVENT, hd);
257 if (ret) {
258 ALOGE("Failed to flip buffer for crtc %d",
259 hd->active_crtc);
260 return ret;
261 }
262
263 do {
264 ret = select(hd->ctx->fd + 1, &fds, NULL, NULL, NULL);
265 } while (ret == -1 && errno == EINTR);
266
267 if (ret != 1) {
268 ALOGE("Failed waiting for flip to complete\n");
269 return -EINVAL;
270 }
271 drmHandleEvent(hd->ctx->fd, &event_context);
272
273 return 0;
274}
275
Sean Paul3bc48e82015-01-23 01:41:13 -0500276static int hwc_wait_and_set(struct hwc_drm_display *hd,
277 struct hwc_drm_bo *buf)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500278{
Sean Paulaea15c22015-02-09 02:24:11 -0500279 struct drm_gem_close args;
280 int ret, i;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500281
Sean Paulaea15c22015-02-09 02:24:11 -0500282 ret = drmModeAddFB2(hd->ctx->fd, buf->width,
Sean Paul3bc48e82015-01-23 01:41:13 -0500283 buf->height, buf->format, buf->gem_handles, buf->pitches,
284 buf->offsets, &buf->fb_id, 0);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500285 if (ret) {
286 ALOGE("could not create drm fb %d", ret);
287 return ret;
288 }
289
Sean Paul3bc48e82015-01-23 01:41:13 -0500290 if (buf->acquire_fence_fd >= 0) {
291 ret = sync_wait(buf->acquire_fence_fd, -1);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500292 if (ret) {
293 ALOGE("Failed to wait for acquire %d", ret);
294 return ret;
295 }
296 }
297
Sean Paul3bc48e82015-01-23 01:41:13 -0500298 ret = hwc_flip(hd, buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500299 if (ret) {
300 ALOGE("Failed to perform flip\n");
301 return ret;
302 }
303
304 if (hd->front.fb_id) {
Sean Paulaea15c22015-02-09 02:24:11 -0500305 ret = drmModeRmFB(hd->ctx->fd, hd->front.fb_id);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500306 if (ret) {
307 ALOGE("Failed to rm fb from front %d", ret);
308 return ret;
309 }
310 }
Sean Paulaea15c22015-02-09 02:24:11 -0500311
312 memset(&args, 0, sizeof(args));
313 for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
314 if (!hd->front.gem_handles[i])
315 continue;
316 args.handle = hd->front.gem_handles[i];
317 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
318 }
Sean Paul3bc48e82015-01-23 01:41:13 -0500319 hd->front = *buf;
320
Sean Paul9aa5ad32015-01-22 15:47:54 -0500321 return ret;
322}
323
324static void *hwc_set_worker(void *arg)
325{
326 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
327 int ret;
328
329 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
330
Sean Paul9aa5ad32015-01-22 15:47:54 -0500331 do {
Sean Paul3bc48e82015-01-23 01:41:13 -0500332 struct hwc_drm_bo buf;
333
334 ret = pthread_mutex_lock(&hd->set_worker.lock);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500335 if (ret) {
Sean Paul3bc48e82015-01-23 01:41:13 -0500336 ALOGE("Failed to lock set lock %d", ret);
337 return NULL;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500338 }
339
Sean Paul3bc48e82015-01-23 01:41:13 -0500340 if (hd->set_worker.exit)
341 goto out;
342
343 if (hd->buf_queue.empty()) {
344 ret = pthread_cond_wait(&hd->set_worker.cond,
345 &hd->set_worker.lock);
346 if (ret) {
347 ALOGE("Failed to wait on condition %d", ret);
348 goto out;
349 }
350 }
351
352 buf = hd->buf_queue.front();
353 hd->buf_queue.pop();
354
355 ret = pthread_mutex_unlock(&hd->set_worker.lock);
356 if (ret) {
357 ALOGE("Failed to unlock set lock %d", ret);
358 return NULL;
359 }
360
361 ret = hwc_wait_and_set(hd, &buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500362 if (ret)
363 ALOGE("Failed to wait and set %d", ret);
Sean Paulf1dc1912015-01-24 01:34:31 -0500364
365 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
366 if (ret)
367 ALOGE("Failed to increment sync timeline %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500368 } while (true);
369
Sean Paul3bc48e82015-01-23 01:41:13 -0500370out:
Sean Paul9aa5ad32015-01-22 15:47:54 -0500371 ret = pthread_mutex_unlock(&hd->set_worker.lock);
Sean Paul3bc48e82015-01-23 01:41:13 -0500372 if (ret)
373 ALOGE("Failed to unlock set lock while exiting %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500374
375 return NULL;
376}
377
Sean Paule0c4c3d2015-01-20 16:56:04 -0500378static int hwc_set_display(hwc_context_t *ctx, int display,
379 hwc_display_contents_1_t* display_contents)
380{
381 struct hwc_drm_display *hd = NULL;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500382 hwc_layer_1_t *layer = NULL;
Sean Paul9b1bb842015-01-23 01:11:58 -0500383 struct hwc_drm_bo buf;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500384 int ret, i;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500385 uint32_t fb_id;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500386
Sean Paul9b1bb842015-01-23 01:11:58 -0500387 memset(&buf, 0, sizeof(buf));
388
Sean Paule0c4c3d2015-01-20 16:56:04 -0500389 ret = hwc_get_drm_display(ctx, display, &hd);
390 if (ret)
391 return ret;
392
393 if (!hd->active_crtc) {
394 ALOGE("There is no active crtc for display %d", display);
395 return -ENOENT;
396 }
397
398 /*
399 * TODO: We can only support one hw layer atm, so choose either the
400 * first one or the framebuffer target.
401 */
402 if (!display_contents->numHwLayers) {
403 return 0;
404 } else if (display_contents->numHwLayers == 1) {
405 layer = &display_contents->hwLayers[0];
406 } else {
407 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
408 layer = &display_contents->hwLayers[i];
409 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
410 break;
411 }
412 if (i == (int)display_contents->numHwLayers) {
413 ALOGE("Could not find a suitable layer for display %d",
414 display);
415 }
416 }
417
Sean Paul3bc48e82015-01-23 01:41:13 -0500418 ret = hwc_create_bo_from_import(ctx->fd, ctx->import_ctx, layer->handle,
419 &buf);
420 if (ret) {
421 ALOGE("Failed to import handle to drm bo %d", ret);
422 return ret;
423 }
424 buf.acquire_fence_fd = layer->acquireFenceFd;
Sean Paul3bc48e82015-01-23 01:41:13 -0500425
Sean Paul9aa5ad32015-01-22 15:47:54 -0500426 ret = pthread_mutex_lock(&hd->set_worker.lock);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500427 if (ret) {
Sean Paul9aa5ad32015-01-22 15:47:54 -0500428 ALOGE("Failed to lock set lock in set() %d", ret);
429 return ret;
430 }
Sean Paulf1dc1912015-01-24 01:34:31 -0500431
432 /*
433 * TODO: Retire and release can use the same sync point here b/c hwc is
434 * restricted to one layer. Once that is no longer true, this will need
435 * to change
436 */
437 hd->timeline_next++;
438 display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd,
439 "drm_hwc_retire", hd->timeline_next);
440 layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd,
441 "drm_hwc_release", hd->timeline_next);
Sean Paul9b1bb842015-01-23 01:11:58 -0500442 hd->buf_queue.push(buf);
443
Sean Paul9aa5ad32015-01-22 15:47:54 -0500444 ret = pthread_cond_signal(&hd->set_worker.cond);
445 if (ret) {
446 ALOGE("Failed to signal set worker %d", ret);
447 goto out;
448 }
449
450 ret = pthread_mutex_unlock(&hd->set_worker.lock);
451 if (ret) {
452 ALOGE("Failed to unlock set lock in set() %d", ret);
453 return ret;
454 }
455
456 return ret;
457
Sean Paule0c4c3d2015-01-20 16:56:04 -0500458out:
Sean Paul9aa5ad32015-01-22 15:47:54 -0500459 if (pthread_mutex_unlock(&hd->set_worker.lock))
460 ALOGE("Failed to unlock set lock in set error handler");
461
Sean Paule0c4c3d2015-01-20 16:56:04 -0500462 return ret;
463}
464
465static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
466 hwc_display_contents_1_t** display_contents)
467{
468 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
469 int ret = 0, i;
470
Sean Paule0c4c3d2015-01-20 16:56:04 -0500471 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
Sean Pauldffca952015-02-04 10:19:55 -0800472 if (display_contents[i])
473 ret = hwc_set_display(ctx, i, display_contents[i]);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500474 }
475
476 return ret;
477}
478
Sean Pauleb9e75c2015-01-25 23:31:30 -0500479static int hwc_wait_for_vsync(struct hwc_drm_display *hd)
Sean Paule0c4c3d2015-01-20 16:56:04 -0500480{
Sean Pauleb9e75c2015-01-25 23:31:30 -0500481 drmVBlank vblank;
482 int ret;
483 uint32_t high_crtc;
484 int64_t timestamp;
485
486 if (hd->active_pipe == -1)
487 return -EINVAL;
488
489 memset(&vblank, 0, sizeof(vblank));
490
491 high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
492 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
493 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
494 vblank.request.sequence = 1;
495
496 ret = drmWaitVBlank(hd->ctx->fd, &vblank);
497 if (ret) {
498 ALOGE("Failed to wait for vblank %d", ret);
499 return ret;
500 }
501
502 if (hd->ctx->procs->vsync) {
503 timestamp = vblank.reply.tval_sec * 1000 * 1000 * 1000 +
504 vblank.reply.tval_usec * 1000;
505 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
506 }
507
508 return 0;
509}
510
511static void *hwc_vsync_worker(void *arg)
512{
513 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
514 struct hwc_worker *w = &hd->vsync_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500515 int ret;
516
Sean Pauleb9e75c2015-01-25 23:31:30 -0500517 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
518
519 do {
520 ret = pthread_mutex_lock(&w->lock);
521 if (ret) {
522 ALOGE("Failed to lock vsync lock %d", ret);
523 return NULL;
524 }
525
526 if (hd->active_pipe == -1) {
527 ALOGE("Pipe is no longer active, disable events");
528 hd->enable_vsync_events = false;
529 }
530
531 if (!hd->enable_vsync_events) {
532 ret = pthread_cond_wait(&w->cond, &w->lock);
533 if (ret) {
534 ALOGE("Failed to wait on vsync cond %d", ret);
535 break;
536 }
537 }
538
539 if (w->exit)
540 break;
541
542 ret = pthread_mutex_unlock(&w->lock);
543 if (ret) {
544 ALOGE("Failed to unlock vsync lock %d", ret);
545 return NULL;
546 }
547
548 if (!hd->enable_vsync_events)
549 continue;
550
551 ret = hwc_wait_for_vsync(hd);
552 if (ret)
553 ALOGE("Failed to wait for vsync %d", ret);
554
555 } while (true);
556
557out:
558 ret = pthread_mutex_unlock(&hd->set_worker.lock);
559 if (ret)
560 ALOGE("Failed to unlock set lock while exiting %d", ret);
561
562 return NULL;
563}
564
565static int hwc_event_control(struct hwc_composer_device_1* dev, int display,
566 int event, int enabled)
567{
568 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
569 struct hwc_drm_display *hd = NULL;
570 int ret;
571
572 ret = hwc_get_drm_display(ctx, display, &hd);
573 if (ret)
574 return ret;
575
576 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
577 return -EINVAL;
578
579 if (hd->active_pipe == -1) {
580 ALOGD("Can't service events for display %d, no pipe", display);
581 return -EINVAL;
582 }
583
584 ret = pthread_mutex_lock(&hd->vsync_worker.lock);
585 if (ret) {
586 ALOGE("Failed to lock vsync lock %d", ret);
587 return ret;
588 }
589
590 hd->enable_vsync_events = !!enabled;
591
592 ret = pthread_cond_signal(&hd->vsync_worker.cond);
593 if (ret) {
594 ALOGE("Failed to signal vsync thread %d", ret);
595 goto out;
596 }
597
598 ret = pthread_mutex_unlock(&hd->vsync_worker.lock);
599 if (ret) {
600 ALOGE("Failed to unlock vsync lock %d", ret);
601 return ret;
602 }
603
Sean Paule0c4c3d2015-01-20 16:56:04 -0500604 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500605
606out:
607 if (pthread_mutex_unlock(&hd->vsync_worker.lock))
608 ALOGE("Failed to unlock vsync worker in error path");
609
610 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500611}
612
613static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display,
614 int mode)
615{
616 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
617 struct hwc_drm_display *hd = NULL;
618 drmModeConnectorPtr c;
619 int ret, i;
620 uint32_t dpms_prop = 0;
621 uint64_t dpms_value = 0;
622
623 ret = hwc_get_drm_display(ctx, display, &hd);
624 if (ret)
625 return ret;
626
627 c = drmModeGetConnector(ctx->fd, hd->connector_id);
628 if (!c) {
629 ALOGE("Failed to get connector %d", display);
630 return -ENODEV;
631 }
632
633 for (i = 0; !dpms_prop && i < c->count_props; i++) {
634 drmModePropertyPtr p;
635
636 p = drmModeGetProperty(ctx->fd, c->props[i]);
637 if (!p)
638 continue;
639
640 if (!strcmp(p->name, "DPMS"))
641 dpms_prop = c->props[i];
642
643 drmModeFreeProperty(p);
644 }
645 if (!dpms_prop) {
646 ALOGE("Failed to get DPMS property from display %d", display);
647 ret = -ENOENT;
648 goto out;
649 }
650
651 switch(mode) {
652 case HWC_POWER_MODE_OFF:
653 dpms_value = DRM_MODE_DPMS_OFF;
654 break;
655
656 /* We can't support dozing right now, so go full on */
657 case HWC_POWER_MODE_DOZE:
658 case HWC_POWER_MODE_DOZE_SUSPEND:
659 case HWC_POWER_MODE_NORMAL:
660 dpms_value = DRM_MODE_DPMS_ON;
661 break;
662 };
663
664 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id,
665 dpms_prop, dpms_value);
666 if (ret) {
667 ALOGE("Failed to set DPMS property for display %d", display);
668 goto out;
669 }
670
671out:
672 drmModeFreeConnector(c);
673 return ret;
674}
675
676static int hwc_query(struct hwc_composer_device_1 */* dev */, int what,
677 int *value)
678{
679 switch(what) {
680 case HWC_BACKGROUND_LAYER_SUPPORTED:
681 *value = 0; /* TODO: We should do this */
682 break;
683 case HWC_VSYNC_PERIOD:
684 ALOGW("Query for deprecated vsync value, returning 60Hz");
685 *value = 1000 * 1000 * 1000 / 60;
686 break;
687 case HWC_DISPLAY_TYPES_SUPPORTED:
688 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
689 break;
690 }
691 return 0;
692}
693
694static void hwc_register_procs(struct hwc_composer_device_1* dev,
695 hwc_procs_t const* procs)
696{
697 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
698
699 ctx->procs = procs;
700}
701
702static int hwc_get_display_configs(struct hwc_composer_device_1* dev,
703 int display, uint32_t* configs, size_t* numConfigs)
704{
705 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
706 struct hwc_drm_display *hd = NULL;
707 drmModeConnectorPtr c;
708 int ret = 0, i;
709
710 if (!*numConfigs)
711 return 0;
712
713 ret = hwc_get_drm_display(ctx, display, &hd);
714 if (ret)
715 return ret;
716
717 c = drmModeGetConnector(ctx->fd, hd->connector_id);
718 if (!c) {
719 ALOGE("Failed to get connector %d", display);
720 return -ENODEV;
721 }
722
Sean Paula4283c52015-02-04 10:08:00 -0800723 if (hd->configs) {
Sean Paule0c4c3d2015-01-20 16:56:04 -0500724 free(hd->configs);
Sean Paula4283c52015-02-04 10:08:00 -0800725 hd->configs = NULL;
726 }
727
728 if (c->connection == DRM_MODE_DISCONNECTED) {
729 ret = -ENODEV;
730 goto out;
731 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500732
Sean Paule0c4c3d2015-01-20 16:56:04 -0500733 hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes,
734 sizeof(*hd->configs));
735 if (!hd->configs) {
736 ALOGE("Failed to allocate config list for display %d", display);
737 ret = -ENOMEM;
738 hd->num_configs = 0;
739 goto out;
740 }
741
742 for (i = 0; i < c->count_modes; i++) {
743 drmModeModeInfoPtr m = &hd->configs[i];
744
745 memcpy(m, &c->modes[i], sizeof(*m));
746
747 if (i < (int)*numConfigs)
748 configs[i] = i;
749 }
750
751 hd->num_configs = c->count_modes;
752 *numConfigs = MIN(c->count_modes, *numConfigs);
753
754out:
755 drmModeFreeConnector(c);
756 return ret;
757}
758
759static int hwc_check_config_valid(struct hwc_context_t *ctx,
760 drmModeConnectorPtr connector, int display,
761 int config_idx)
762{
763 struct hwc_drm_display *hd = NULL;
764 drmModeModeInfoPtr m = NULL;
765 int ret = 0, i;
766
767 ret = hwc_get_drm_display(ctx, display, &hd);
768 if (ret)
769 return ret;
770
771 /* Make sure the requested config is still valid for the display */
772 for (i = 0; i < connector->count_modes; i++) {
773 if (hwc_mode_is_equal(&connector->modes[i],
774 &hd->configs[config_idx])) {
775 m = &hd->configs[config_idx];
776 break;
777 }
778 }
779 if (!m)
780 return -ENOENT;
781
782 return 0;
783}
784
785static int hwc_get_display_attributes(struct hwc_composer_device_1* dev,
786 int display, uint32_t config, const uint32_t* attributes,
787 int32_t* values)
788{
789 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
790 struct hwc_drm_display *hd = NULL;
791 drmModeConnectorPtr c;
792 drmModeModeInfoPtr m;
793 int ret, i;
794
795 ret = hwc_get_drm_display(ctx, display, &hd);
796 if (ret)
797 return ret;
798
799 if (config >= hd->num_configs) {
800 ALOGE("Requested config is out-of-bounds %d %d", config,
801 hd->num_configs);
802 return -EINVAL;
803 }
804
805 c = drmModeGetConnector(ctx->fd, hd->connector_id);
806 if (!c) {
807 ALOGE("Failed to get connector %d", display);
808 return -ENODEV;
809 }
810
811 ret = hwc_check_config_valid(ctx, c, display, (int)config);
812 if (ret) {
813 ALOGE("Provided config is no longer valid %u", config);
814 goto out;
815 }
816
817 m = &hd->configs[config];
818 for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
819 switch(attributes[i]) {
820 case HWC_DISPLAY_VSYNC_PERIOD:
821 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
822 break;
823 case HWC_DISPLAY_WIDTH:
824 values[i] = m->hdisplay;
825 break;
826 case HWC_DISPLAY_HEIGHT:
827 values[i] = m->vdisplay;
828 break;
829 case HWC_DISPLAY_DPI_X:
830 /* Dots per 1000 inches */
831 values[i] = c->mmWidth ?
832 (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
833 break;
834 case HWC_DISPLAY_DPI_Y:
835 /* Dots per 1000 inches */
836 values[i] = c->mmHeight ?
837 (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
838 break;
839 }
840 }
841
842out:
843 drmModeFreeConnector(c);
844 return ret;
845}
846
847static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display)
848{
849 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
850 struct hwc_drm_display *hd = NULL;
Sean Paulfa406a12015-02-04 10:05:44 -0800851 int ret, i, index = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500852
853 ret = hwc_get_drm_display(ctx, display, &hd);
854 if (ret)
855 return ret;
856
Sean Paulfa406a12015-02-04 10:05:44 -0800857 /* Find the current mode in the config list */
858 for (i = 0; i < (int)hd->num_configs; i++) {
859 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
860 index = i;
861 break;
862 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500863 }
864
Sean Paulfa406a12015-02-04 10:05:44 -0800865 return index;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500866}
867
868static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id)
869{
870 int i;
871
872 for (i = 0; i < MAX_NUM_DISPLAYS; i++) {
873 if (ctx->displays[i].active_crtc == crtc_id)
874 return true;
875 }
876 return false;
877}
878
879static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
880 uint32_t encoder_id, uint32_t *crtc_id)
881{
882 drmModeEncoderPtr e;
883 int ret, i;
884
885 e = drmModeGetEncoder(ctx->fd, encoder_id);
886 if (!e) {
887 ALOGE("Failed to get encoder for connector %d", encoder_id);
888 return -ENODEV;
889 }
890
891 /* First try to use the currently-bound crtc */
892 if (e->crtc_id) {
893 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
894 *crtc_id = e->crtc_id;
895 ret = 0;
896 goto out;
897 }
898 }
899
900 /* Try to find a possible crtc which will work */
901 for (i = 0; i < r->count_crtcs; i++) {
902 if (!(e->possible_crtcs & (1 << i)))
903 continue;
904
905 /* We've already tried this earlier */
906 if (e->crtc_id == r->crtcs[i])
907 continue;
908
909 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
910 *crtc_id = r->crtcs[i];
911 ret = 0;
912 goto out;
913 }
914 }
915
916 /* We can't use the encoder, but nothing went wrong, try another one */
917 ret = -EAGAIN;
918
919out:
920 drmModeFreeEncoder(e);
921 return ret;
922}
923
924static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display,
925 int index)
926{
927 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
928 struct hwc_drm_display *hd = NULL;
929 drmModeResPtr r = NULL;
930 drmModeConnectorPtr c;
931 uint32_t crtc_id = 0;
932 int ret, i;
933 bool new_crtc, new_encoder;
934
935 ret = hwc_get_drm_display(ctx, display, &hd);
936 if (ret)
937 return ret;
938
939 c = drmModeGetConnector(ctx->fd, hd->connector_id);
940 if (!c) {
941 ALOGE("Failed to get connector %d", display);
942 return -ENODEV;
943 }
944
945 if (c->connection == DRM_MODE_DISCONNECTED) {
946 ALOGE("Tried to configure a disconnected display %d", display);
947 ret = -ENODEV;
948 goto out;
949 }
950
Sean Paulfa406a12015-02-04 10:05:44 -0800951 if (index >= c->count_modes) {
952 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500953 ret = -ENOENT;
954 goto out;
955 }
956
957 r = drmModeGetResources(ctx->fd);
958 if (!r) {
959 ALOGE("Failed to get drm resources");
960 goto out;
961 }
962
963 /* We no longer have an active_crtc */
964 hd->active_crtc = 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500965 hd->active_pipe = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500966
967 /* First, try to use the currently-connected encoder */
968 if (c->encoder_id) {
969 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
970 if (ret && ret != -EAGAIN) {
971 ALOGE("Encoder try failed %d", ret);
972 goto out;
973 }
974 }
975
976 /* We couldn't find a crtc with the attached encoder, try the others */
977 if (!crtc_id) {
978 for (i = 0; i < c->count_encoders; i++) {
979 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
980 if (!ret) {
981 break;
982 } else if (ret != -EAGAIN) {
983 ALOGE("Encoder try failed %d", ret);
984 goto out;
985 }
986 }
987 if (!crtc_id) {
988 ALOGE("Couldn't find valid crtc to modeset");
989 ret = -EINVAL;
990 goto out;
991 }
992 }
993
994 hd->active_crtc = crtc_id;
Sean Paulfa406a12015-02-04 10:05:44 -0800995
996 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
Sean Paule0c4c3d2015-01-20 16:56:04 -0500997
Sean Pauleb9e75c2015-01-25 23:31:30 -0500998 /* Find the pipe corresponding to the crtc_id */
999 for (i = 0; i < r->count_crtcs; i++) {
1000 /* We've already tried this earlier */
1001 if (r->crtcs[i] == crtc_id) {
1002 hd->active_pipe = i;
1003 break;
1004 }
1005 }
1006 /* This should never happen... hehehe */
1007 if (hd->active_pipe == -1) {
1008 ALOGE("Active crtc was not found in resources!!");
1009 ret = -ENODEV;
1010 goto out;
1011 }
1012
Sean Paule0c4c3d2015-01-20 16:56:04 -05001013 /* TODO: Once we have atomic, set the crtc timing info here */
1014
1015out:
1016 if (r)
1017 drmModeFreeResources(r);
1018
1019 drmModeFreeConnector(c);
1020 return ret;
1021}
1022
Sean Paul9aa5ad32015-01-22 15:47:54 -05001023static int hwc_destroy_worker(struct hwc_worker *worker)
1024{
1025 int ret;
1026
1027 ret = pthread_mutex_lock(&worker->lock);
1028 if (ret) {
1029 ALOGE("Failed to lock in destroy() %d", ret);
1030 return ret;
1031 }
1032
1033 worker->exit = true;
1034
1035 ret |= pthread_cond_signal(&worker->cond);
1036 if (ret)
1037 ALOGE("Failed to signal cond in destroy() %d", ret);
1038
1039 ret |= pthread_mutex_unlock(&worker->lock);
1040 if (ret)
1041 ALOGE("Failed to unlock in destroy() %d", ret);
1042
1043 ret |= pthread_join(worker->thread, NULL);
1044 if (ret && ret != ESRCH)
1045 ALOGE("Failed to join thread in destroy() %d", ret);
1046
1047 return ret;
1048}
1049
1050static void hwc_destroy_display(struct hwc_drm_display *hd)
1051{
1052 int ret;
1053
1054 if (hwc_destroy_worker(&hd->set_worker))
1055 ALOGE("Destroy set worker failed");
Sean Pauleb9e75c2015-01-25 23:31:30 -05001056
1057 if (hwc_destroy_worker(&hd->vsync_worker))
1058 ALOGE("Destroy vsync worker failed");
Sean Paul9aa5ad32015-01-22 15:47:54 -05001059}
1060
Sean Paule0c4c3d2015-01-20 16:56:04 -05001061static int hwc_device_close(struct hw_device_t *dev)
1062{
1063 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulcd36a9e2015-01-22 18:01:18 -05001064 int ret, i;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001065
Sean Paul9aa5ad32015-01-22 15:47:54 -05001066 for (i = 0; i < MAX_NUM_DISPLAYS; i++)
1067 hwc_destroy_display(&ctx->displays[i]);
1068
1069 drmClose(ctx->fd);
Sean Paulcd36a9e2015-01-22 18:01:18 -05001070
1071 ret = hwc_import_destroy(ctx->import_ctx);
1072 if (ret)
1073 ALOGE("Could not destroy import %d", ret);
1074
Sean Paule0c4c3d2015-01-20 16:56:04 -05001075 free(ctx);
1076
1077 return 0;
1078}
1079
Sean Paul9aa5ad32015-01-22 15:47:54 -05001080static int hwc_initialize_worker(struct hwc_drm_display *hd,
1081 struct hwc_worker *worker, void *(*routine)(void*))
1082{
1083 int ret;
1084
1085 ret = pthread_cond_init(&worker->cond, NULL);
1086 if (ret) {
1087 ALOGE("Failed to create worker condition %d", ret);
1088 return ret;
1089 }
1090
1091 ret = pthread_mutex_init(&worker->lock, NULL);
1092 if (ret) {
1093 ALOGE("Failed to initialize worker lock %d", ret);
1094 goto err_cond;
1095 }
1096
1097 worker->exit = false;
1098
1099 ret = pthread_create(&worker->thread, NULL, routine, hd);
1100 if (ret) {
1101 ALOGE("Could not create worker thread %d", ret);
1102 goto err_lock;
1103 }
1104 return 0;
1105
1106err_lock:
1107 pthread_mutex_destroy(&worker->lock);
1108err_cond:
1109 pthread_cond_destroy(&worker->cond);
1110 return ret;
1111}
1112
Sean Paul24a26e32015-02-04 10:34:47 -08001113/*
1114 * TODO: This function sets the active config to the first one in the list. This
1115 * should be fixed such that it selects the preferred mode for the display, or
1116 * some other, saner, method of choosing the config.
1117 */
1118static int hwc_set_initial_config(struct hwc_drm_display *hd)
1119{
1120 int ret;
1121 uint32_t config;
1122 size_t num_configs = 1;
1123
1124 ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1125 &num_configs);
Sean Paulaea15c22015-02-09 02:24:11 -05001126 if (ret || !num_configs)
Sean Paul24a26e32015-02-04 10:34:47 -08001127 return 0;
1128
1129 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1130 if (ret) {
1131 ALOGE("Failed to set active config d=%d ret=%d", hd->display,
1132 ret);
1133 return ret;
1134 }
1135
1136 return ret;
1137}
1138
Sean Paule0c4c3d2015-01-20 16:56:04 -05001139static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1140 uint32_t connector_id)
1141{
1142 struct hwc_drm_display *hd = NULL;
1143 int ret;
1144
1145 ret = hwc_get_drm_display(ctx, display, &hd);
1146 if (ret)
1147 return ret;
1148
Sean Paul9aa5ad32015-01-22 15:47:54 -05001149 hd->ctx = ctx;
1150 hd->display = display;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001151 hd->active_pipe = -1;
Sean Paulefb20cb2015-02-04 09:29:15 -08001152 hd->initial_modeset_required = true;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001153 hd->connector_id = connector_id;
1154
Sean Paulf1dc1912015-01-24 01:34:31 -05001155 ret = sw_sync_timeline_create();
1156 if (ret < 0) {
1157 ALOGE("Failed to create sw sync timeline %d", ret);
1158 return ret;
1159 }
1160 hd->timeline_fd = ret;
1161
Sean Paul24a26e32015-02-04 10:34:47 -08001162 ret = hwc_set_initial_config(hd);
1163 if (ret) {
1164 ALOGE("Failed to set initial config for d=%d ret=%d", display,
1165 ret);
1166 return ret;
1167 }
1168
Sean Paul9aa5ad32015-01-22 15:47:54 -05001169 ret = hwc_initialize_worker(hd, &hd->set_worker, hwc_set_worker);
1170 if (ret) {
1171 ALOGE("Failed to create set worker %d\n", ret);
1172 return ret;
1173 }
1174
Sean Pauleb9e75c2015-01-25 23:31:30 -05001175 ret = hwc_initialize_worker(hd, &hd->vsync_worker, hwc_vsync_worker);
1176 if (ret) {
1177 ALOGE("Failed to create vsync worker %d", ret);
1178 goto err;
1179 }
1180
Sean Paule0c4c3d2015-01-20 16:56:04 -05001181 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001182
1183err:
1184 if (hwc_destroy_worker(&hd->set_worker))
1185 ALOGE("Failed to destroy set worker");
1186
1187 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001188}
1189
1190static int hwc_enumerate_displays(struct hwc_context_t *ctx)
1191{
1192 struct hwc_drm_display *panel_hd;
1193 drmModeResPtr res;
1194 drmModeConnectorPtr *conn_list;
1195 int ret = 0, i, j;
1196
1197 res = drmModeGetResources(ctx->fd);
1198 if (!res) {
1199 ALOGE("Failed to get drm resources");
1200 return -ENODEV;
1201 }
1202
1203 conn_list = (drmModeConnector **)calloc(res->count_connectors,
1204 sizeof(*conn_list));
1205 if (!conn_list) {
1206 ALOGE("Failed to allocate connector list");
1207 ret = -ENOMEM;
1208 goto out;
1209 }
1210
1211 for (i = 0; i < res->count_connectors; i++) {
1212 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1213 if (!conn_list[i]) {
1214 ALOGE("Failed to get connector %d", res->connectors[i]);
1215 ret = -ENODEV;
1216 goto out;
1217 }
1218 }
1219
1220 ctx->num_displays = 0;
1221
1222 /* Find a connected, panel type connector for display 0 */
1223 for (i = 0; i < res->count_connectors; i++) {
1224 drmModeConnectorPtr c = conn_list[i];
1225
1226 for (j = 0; j < ARRAY_SIZE(panel_types); j++) {
1227 if (c->connector_type == panel_types[j] &&
1228 c->connection == DRM_MODE_CONNECTED)
1229 break;
1230 }
1231 if (j == ARRAY_SIZE(panel_types))
1232 continue;
1233
1234 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1235 ctx->num_displays++;
1236 break;
1237 }
1238
1239 ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1240 if (ret)
1241 goto out;
1242
1243 /* Fill in the other displays */
1244 for (i = 0; i < res->count_connectors; i++) {
1245 drmModeConnectorPtr c = conn_list[i];
1246
1247 if (panel_hd->connector_id == c->connector_id)
1248 continue;
1249
1250 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1251 ctx->num_displays++;
1252 }
1253
1254out:
1255 for (i = 0; i < res->count_connectors; i++) {
1256 if (conn_list[i])
1257 drmModeFreeConnector(conn_list[i]);
1258 }
1259 free(conn_list);
1260
1261 if (res)
1262 drmModeFreeResources(res);
1263
1264 return ret;
1265}
1266
1267static int hwc_device_open(const struct hw_module_t* module, const char* name,
1268 struct hw_device_t** dev)
1269{
1270 int ret = 0;
1271 struct hwc_context_t *ctx;
1272
1273 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1274 ALOGE("Invalid module name- %s", name);
1275 return -EINVAL;
1276 }
1277
Sean Paul9b1bb842015-01-23 01:11:58 -05001278 ctx = new hwc_context_t();
Sean Paule0c4c3d2015-01-20 16:56:04 -05001279 if (!ctx) {
1280 ALOGE("Failed to allocate hwc context");
1281 return -ENOMEM;
1282 }
1283
Sean Paulcd36a9e2015-01-22 18:01:18 -05001284 ret = hwc_import_init(&ctx->import_ctx);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001285 if (ret) {
Sean Paulcd36a9e2015-01-22 18:01:18 -05001286 ALOGE("Failed to initialize import context");
Sean Paule0c4c3d2015-01-20 16:56:04 -05001287 goto out;
1288 }
1289
1290 /* TODO: Use drmOpenControl here instead */
1291 ctx->fd = open(HWCOMPOSER_DRM_DEVICE, O_RDWR);
1292 if (ctx->fd < 0) {
1293 ALOGE("Failed to open dri- %s", strerror(-errno));
1294 goto out;
1295 }
1296
1297 ret = drmSetMaster(ctx->fd);
1298 if (ret) {
1299 ALOGE("Failed to set hwcomposer as drm master %d", ret);
1300 goto out;
1301 }
1302
1303 ret = hwc_enumerate_displays(ctx);
1304 if (ret) {
1305 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1306 goto out;
1307 }
1308
1309 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1310 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1311 ctx->device.common.module = const_cast<hw_module_t*>(module);
1312 ctx->device.common.close = hwc_device_close;
1313
1314 ctx->device.prepare = hwc_prepare;
1315 ctx->device.set = hwc_set;
1316 ctx->device.eventControl = hwc_event_control;
1317 ctx->device.setPowerMode = hwc_set_power_mode;
1318 ctx->device.query = hwc_query;
1319 ctx->device.registerProcs = hwc_register_procs;
1320 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1321 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1322 ctx->device.getActiveConfig = hwc_get_active_config;
1323 ctx->device.setActiveConfig = hwc_set_active_config;
1324 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1325
1326 *dev = &ctx->device.common;
1327
1328 return 0;
1329out:
1330 if (ctx->fd >= 0)
1331 close(ctx->fd);
1332
1333 free(ctx);
1334 return ret;
1335}
1336
1337static struct hw_module_methods_t hwc_module_methods = {
1338 open: hwc_device_open
1339};
1340
1341hwc_module_t HAL_MODULE_INFO_SYM = {
1342 common: {
1343 tag: HARDWARE_MODULE_TAG,
1344 version_major: 1,
1345 version_minor: 0,
1346 id: HWC_HARDWARE_MODULE_ID,
1347 name: "DRM hwcomposer module",
1348 author: "The Android Open Source Project",
1349 methods: &hwc_module_methods,
1350 dso: NULL,
1351 reserved: { 0 },
1352 }
1353};