blob: 77caf900e27a9580ec9c6a16bb2a4e6bd6373b18 [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>
Allen Martin3d3f70a2015-02-21 21:20:17 -080021#include <list>
Sean Paule0c4c3d2015-01-20 16:56:04 -050022#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050023#include <sys/resource.h>
24#include <pthread.h>
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
Lauri Peltonen64717b22015-02-04 16:55:31 +020034#include <cutils/properties.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050035#include <sync/sync.h>
Sean Paulf1dc1912015-01-24 01:34:31 -050036#include <sw_sync.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050037
Sean Paulcd36a9e2015-01-22 18:01:18 -050038#include "drm_hwcomposer.h"
Sean Paule0c4c3d2015-01-20 16:56:04 -050039
40#define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0]))
41
42#define HWCOMPOSER_DRM_DEVICE "/dev/dri/card0"
43#define MAX_NUM_DISPLAYS 3
44#define UM_PER_INCH 25400
45
46static const uint32_t panel_types[] = {
47 DRM_MODE_CONNECTOR_LVDS,
48 DRM_MODE_CONNECTOR_eDP,
49 DRM_MODE_CONNECTOR_DSI,
50};
51
Sean Paul9aa5ad32015-01-22 15:47:54 -050052struct hwc_worker {
53 pthread_t thread;
54 pthread_mutex_t lock;
55 pthread_cond_t cond;
56 bool exit;
57};
58
Sean Paule0c4c3d2015-01-20 16:56:04 -050059struct hwc_drm_display {
Sean Paul9aa5ad32015-01-22 15:47:54 -050060 struct hwc_context_t *ctx;
61 int display;
62
Sean Paule0c4c3d2015-01-20 16:56:04 -050063 uint32_t connector_id;
64
65 drmModeModeInfoPtr configs;
66 uint32_t num_configs;
67
Sean Paulfa406a12015-02-04 10:05:44 -080068 drmModeModeInfo active_mode;
Sean Paule0c4c3d2015-01-20 16:56:04 -050069 uint32_t active_crtc;
Sean Pauleb9e75c2015-01-25 23:31:30 -050070 int active_pipe;
Sean Paulefb20cb2015-02-04 09:29:15 -080071 bool initial_modeset_required;
Sean Paul9aa5ad32015-01-22 15:47:54 -050072
73 struct hwc_worker set_worker;
74
Allen Martin3d3f70a2015-02-21 21:20:17 -080075 std::list<struct hwc_drm_bo> buf_queue;
Sean Paul9aa5ad32015-01-22 15:47:54 -050076 struct hwc_drm_bo front;
Sean Paulf1dc1912015-01-24 01:34:31 -050077
78 int timeline_fd;
79 unsigned timeline_next;
Sean Pauleb9e75c2015-01-25 23:31:30 -050080
81 struct hwc_worker vsync_worker;
82 bool enable_vsync_events;
Sean Paule0c4c3d2015-01-20 16:56:04 -050083};
84
85struct hwc_context_t {
86 hwc_composer_device_1_t device;
87
88 int fd;
Sean Paule0c4c3d2015-01-20 16:56:04 -050089
90 hwc_procs_t const *procs;
Sean Paulcd36a9e2015-01-22 18:01:18 -050091 struct hwc_import_context *import_ctx;
Sean Paule0c4c3d2015-01-20 16:56:04 -050092
93 struct hwc_drm_display displays[MAX_NUM_DISPLAYS];
94 int num_displays;
95};
96
97static int hwc_get_drm_display(struct hwc_context_t *ctx, int display,
98 struct hwc_drm_display **hd)
99{
100 if (display >= MAX_NUM_DISPLAYS) {
101 ALOGE("Requested display is out-of-bounds %d %d", display,
102 MAX_NUM_DISPLAYS);
103 return -EINVAL;
104 }
105 *hd = &ctx->displays[display];
106 return 0;
107}
108
109static int hwc_prepare_layer(hwc_layer_1_t *layer)
110{
111 /* TODO: We can't handle background right now, defer to sufaceFlinger */
112 if (layer->compositionType == HWC_BACKGROUND) {
113 layer->compositionType = HWC_FRAMEBUFFER;
114 ALOGV("Can't handle background layers yet");
115
116 /* TODO: Support sideband compositions */
117 } else if (layer->compositionType == HWC_SIDEBAND) {
118 layer->compositionType = HWC_FRAMEBUFFER;
119 ALOGV("Can't handle sideband content yet");
120 }
121
122 layer->hints = 0;
123
124 /* TODO: Handle cursor by setting compositionType=HWC_CURSOR_OVERLAY */
125 if (layer->flags & HWC_IS_CURSOR_LAYER) {
126 ALOGV("Can't handle async cursors yet");
127 }
128
129 /* TODO: Handle transformations */
130 if (layer->transform) {
131 ALOGV("Can't handle transformations yet");
132 }
133
134 /* TODO: Handle blending & plane alpha*/
135 if (layer->blending == HWC_BLENDING_PREMULT ||
136 layer->blending == HWC_BLENDING_COVERAGE) {
137 ALOGV("Can't handle blending yet");
138 }
139
140 /* TODO: Handle cropping & scaling */
141
142 return 0;
143}
144
145static int hwc_prepare(hwc_composer_device_1_t */* dev */, size_t num_displays,
146 hwc_display_contents_1_t** display_contents)
147{
148 int ret = 0, i, j;
149
150 /* TODO: Check flags for HWC_GEOMETRY_CHANGED */
151
152 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
Sean Pauldffca952015-02-04 10:19:55 -0800153
154 if (!display_contents[i])
155 continue;
156
Sean Paule0c4c3d2015-01-20 16:56:04 -0500157 for (j = 0; j < (int)display_contents[i]->numHwLayers; j++) {
158 ret = hwc_prepare_layer(
159 &display_contents[i]->hwLayers[j]);
160 if (ret) {
161 ALOGE("Failed to prepare layer %d:%d", j, i);
162 return ret;
163 }
164 }
165 }
166
167 return ret;
168}
169
Sean Paule0c4c3d2015-01-20 16:56:04 -0500170static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b)
171{
172 return a->clock == b->clock &&
173 a->hdisplay == b->hdisplay &&
174 a->hsync_start == b->hsync_start &&
175 a->hsync_end == b->hsync_end &&
176 a->htotal == b->htotal &&
177 a->hskew == b->hskew &&
178 a->vdisplay == b->vdisplay &&
179 a->vsync_start == b->vsync_start &&
180 a->vsync_end == b->vsync_end &&
181 a->vtotal == b->vtotal &&
182 a->vscan == b->vscan &&
183 a->vrefresh == b->vrefresh &&
184 a->flags == b->flags &&
185 a->type == b->type &&
186 !strcmp(a->name, b->name);
187}
188
Sean Paul9aa5ad32015-01-22 15:47:54 -0500189static int hwc_modeset_required(struct hwc_drm_display *hd,
190 bool *modeset_required)
191{
192 drmModeCrtcPtr crtc;
193 drmModeModeInfoPtr m;
194
Sean Paulefb20cb2015-02-04 09:29:15 -0800195 if (hd->initial_modeset_required) {
196 *modeset_required = true;
197 hd->initial_modeset_required = false;
198 return 0;
199 }
200
Sean Paul9aa5ad32015-01-22 15:47:54 -0500201 crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc);
202 if (!crtc) {
203 ALOGE("Failed to get crtc for display %d", hd->display);
204 return -ENODEV;
205 }
206
Sean Paulfa406a12015-02-04 10:05:44 -0800207 m = &hd->active_mode;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500208
209 /* Do a modeset if we haven't done one, or the mode has changed */
210 if (!crtc->mode_valid || !hwc_mode_is_equal(m, &crtc->mode))
211 *modeset_required = true;
212 else
213 *modeset_required = false;
214
215 drmModeFreeCrtc(crtc);
216
217 return 0;
218}
219
220static void hwc_flip_handler(int /* fd */, unsigned int /* sequence */,
221 unsigned int /* tv_sec */, unsigned int /* tv_usec */,
222 void */* user_data */)
223{
224}
225
Sean Paul9b1bb842015-01-23 01:11:58 -0500226static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500227{
228 fd_set fds;
229 drmEventContext event_context;
230 int ret;
231 bool modeset_required;
232
233 ret = hwc_modeset_required(hd, &modeset_required);
234 if (ret) {
235 ALOGE("Failed to determine if modeset is required %d", ret);
236 return ret;
237 }
238 if (modeset_required) {
Sean Paul9b1bb842015-01-23 01:11:58 -0500239 ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id,
240 0, 0, &hd->connector_id, 1,
Sean Paulfa406a12015-02-04 10:05:44 -0800241 &hd->active_mode);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500242 if (ret) {
243 ALOGE("Modeset failed for crtc %d",
244 hd->active_crtc);
245 return ret;
246 }
247 return 0;
248 }
249
250 FD_ZERO(&fds);
251 FD_SET(hd->ctx->fd, &fds);
252
253 event_context.version = DRM_EVENT_CONTEXT_VERSION;
254 event_context.page_flip_handler = hwc_flip_handler;
255
Sean Paul9b1bb842015-01-23 01:11:58 -0500256 ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id,
Sean Paul9aa5ad32015-01-22 15:47:54 -0500257 DRM_MODE_PAGE_FLIP_EVENT, hd);
258 if (ret) {
259 ALOGE("Failed to flip buffer for crtc %d",
260 hd->active_crtc);
261 return ret;
262 }
263
264 do {
265 ret = select(hd->ctx->fd + 1, &fds, NULL, NULL, NULL);
266 } while (ret == -1 && errno == EINTR);
267
268 if (ret != 1) {
269 ALOGE("Failed waiting for flip to complete\n");
270 return -EINVAL;
271 }
272 drmHandleEvent(hd->ctx->fd, &event_context);
273
274 return 0;
275}
276
Sean Paul3bc48e82015-01-23 01:41:13 -0500277static int hwc_wait_and_set(struct hwc_drm_display *hd,
278 struct hwc_drm_bo *buf)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500279{
Sean Paulaea15c22015-02-09 02:24:11 -0500280 struct drm_gem_close args;
281 int ret, i;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500282
Lauri Peltonen132e0102015-02-12 13:54:33 +0200283 if (buf->acquire_fence_fd >= 0) {
284 ret = sync_wait(buf->acquire_fence_fd, -1);
285 close(buf->acquire_fence_fd);
286 buf->acquire_fence_fd = -1;
287 if (ret) {
288 ALOGE("Failed to wait for acquire %d", ret);
289 return ret;
290 }
291 }
292
Sean Paulaea15c22015-02-09 02:24:11 -0500293 ret = drmModeAddFB2(hd->ctx->fd, buf->width,
Sean Paul3bc48e82015-01-23 01:41:13 -0500294 buf->height, buf->format, buf->gem_handles, buf->pitches,
295 buf->offsets, &buf->fb_id, 0);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500296 if (ret) {
297 ALOGE("could not create drm fb %d", ret);
298 return ret;
299 }
300
Sean Paul3bc48e82015-01-23 01:41:13 -0500301 ret = hwc_flip(hd, buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500302 if (ret) {
303 ALOGE("Failed to perform flip\n");
304 return ret;
305 }
306
307 if (hd->front.fb_id) {
Sean Paulaea15c22015-02-09 02:24:11 -0500308 ret = drmModeRmFB(hd->ctx->fd, hd->front.fb_id);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500309 if (ret) {
310 ALOGE("Failed to rm fb from front %d", ret);
311 return ret;
312 }
313 }
Sean Paulaea15c22015-02-09 02:24:11 -0500314
315 memset(&args, 0, sizeof(args));
316 for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
317 if (!hd->front.gem_handles[i])
318 continue;
Allen Martin3d3f70a2015-02-21 21:20:17 -0800319
320 /* check for duplicate handle in buf_queue */
Allen Martinf4e49b82015-02-22 04:18:17 -0800321 bool found;
Allen Martin3d3f70a2015-02-21 21:20:17 -0800322
323 ret = pthread_mutex_lock(&hd->set_worker.lock);
324 if (ret) {
325 ALOGE("Failed to lock set lock in wait_and_set() %d", ret);
326 continue;
327 }
328
329 found = false;
Allen Martinf4e49b82015-02-22 04:18:17 -0800330 for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin();
Allen Martin3d3f70a2015-02-21 21:20:17 -0800331 bi != hd->buf_queue.end();
332 ++bi)
333 for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++)
Allen Martinf4e49b82015-02-22 04:18:17 -0800334 if (hd->front.gem_handles[i] == bi->gem_handles[j] )
Allen Martin3d3f70a2015-02-21 21:20:17 -0800335 found = true;
336
Allen Martinf4e49b82015-02-22 04:18:17 -0800337 for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); j++)
338 if (hd->front.gem_handles[i] == buf->gem_handles[j])
339 found = true;
340
Allen Martin3d3f70a2015-02-21 21:20:17 -0800341 if (!found) {
342 args.handle = hd->front.gem_handles[i];
343 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
344 }
345 if (pthread_mutex_unlock(&hd->set_worker.lock))
346 ALOGE("Failed to unlock set lock in wait_and_set() %d", ret);
Sean Paulaea15c22015-02-09 02:24:11 -0500347 }
Sean Paul3bc48e82015-01-23 01:41:13 -0500348 hd->front = *buf;
349
Sean Paul9aa5ad32015-01-22 15:47:54 -0500350 return ret;
351}
352
353static void *hwc_set_worker(void *arg)
354{
355 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
356 int ret;
357
358 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
359
Sean Paul9aa5ad32015-01-22 15:47:54 -0500360 do {
Sean Paul3bc48e82015-01-23 01:41:13 -0500361 struct hwc_drm_bo buf;
362
363 ret = pthread_mutex_lock(&hd->set_worker.lock);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500364 if (ret) {
Sean Paul3bc48e82015-01-23 01:41:13 -0500365 ALOGE("Failed to lock set lock %d", ret);
366 return NULL;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500367 }
368
Sean Paul3bc48e82015-01-23 01:41:13 -0500369 if (hd->set_worker.exit)
370 goto out;
371
372 if (hd->buf_queue.empty()) {
373 ret = pthread_cond_wait(&hd->set_worker.cond,
374 &hd->set_worker.lock);
375 if (ret) {
376 ALOGE("Failed to wait on condition %d", ret);
377 goto out;
378 }
379 }
380
381 buf = hd->buf_queue.front();
Allen Martin3d3f70a2015-02-21 21:20:17 -0800382 hd->buf_queue.pop_front();
Sean Paul3bc48e82015-01-23 01:41:13 -0500383
384 ret = pthread_mutex_unlock(&hd->set_worker.lock);
385 if (ret) {
386 ALOGE("Failed to unlock set lock %d", ret);
387 return NULL;
388 }
389
390 ret = hwc_wait_and_set(hd, &buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500391 if (ret)
392 ALOGE("Failed to wait and set %d", ret);
Sean Paulf1dc1912015-01-24 01:34:31 -0500393
394 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
395 if (ret)
396 ALOGE("Failed to increment sync timeline %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500397 } while (true);
398
Sean Paul3bc48e82015-01-23 01:41:13 -0500399out:
Sean Paul9aa5ad32015-01-22 15:47:54 -0500400 ret = pthread_mutex_unlock(&hd->set_worker.lock);
Sean Paul3bc48e82015-01-23 01:41:13 -0500401 if (ret)
402 ALOGE("Failed to unlock set lock while exiting %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500403
404 return NULL;
405}
406
Sean Paule0c4c3d2015-01-20 16:56:04 -0500407static int hwc_set_display(hwc_context_t *ctx, int display,
408 hwc_display_contents_1_t* display_contents)
409{
410 struct hwc_drm_display *hd = NULL;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500411 hwc_layer_1_t *layer = NULL;
Sean Paul9b1bb842015-01-23 01:11:58 -0500412 struct hwc_drm_bo buf;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500413 int ret, i;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500414 uint32_t fb_id;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500415
Sean Paul9b1bb842015-01-23 01:11:58 -0500416 memset(&buf, 0, sizeof(buf));
417
Sean Paule0c4c3d2015-01-20 16:56:04 -0500418 ret = hwc_get_drm_display(ctx, display, &hd);
419 if (ret)
Lauri Peltonen132e0102015-02-12 13:54:33 +0200420 goto out;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500421
422 if (!hd->active_crtc) {
423 ALOGE("There is no active crtc for display %d", display);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200424 ret = -ENOENT;
425 goto out;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500426 }
427
428 /*
429 * TODO: We can only support one hw layer atm, so choose either the
430 * first one or the framebuffer target.
431 */
432 if (!display_contents->numHwLayers) {
433 return 0;
434 } else if (display_contents->numHwLayers == 1) {
435 layer = &display_contents->hwLayers[0];
436 } else {
437 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
438 layer = &display_contents->hwLayers[i];
439 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
440 break;
441 }
442 if (i == (int)display_contents->numHwLayers) {
443 ALOGE("Could not find a suitable layer for display %d",
444 display);
445 }
446 }
447
Allen Martin3d3f70a2015-02-21 21:20:17 -0800448
449 ret = pthread_mutex_lock(&hd->set_worker.lock);
450 if (ret) {
451 ALOGE("Failed to lock set lock in set() %d", ret);
452 goto out;
453 }
454
Sean Paul3bc48e82015-01-23 01:41:13 -0500455 ret = hwc_create_bo_from_import(ctx->fd, ctx->import_ctx, layer->handle,
456 &buf);
457 if (ret) {
458 ALOGE("Failed to import handle to drm bo %d", ret);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200459 goto out;
Sean Paul3bc48e82015-01-23 01:41:13 -0500460 }
461 buf.acquire_fence_fd = layer->acquireFenceFd;
Lauri Peltonen132e0102015-02-12 13:54:33 +0200462 layer->acquireFenceFd = -1;
Sean Paul3bc48e82015-01-23 01:41:13 -0500463
Sean Paulf1dc1912015-01-24 01:34:31 -0500464 /*
465 * TODO: Retire and release can use the same sync point here b/c hwc is
466 * restricted to one layer. Once that is no longer true, this will need
467 * to change
468 */
469 hd->timeline_next++;
470 display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd,
471 "drm_hwc_retire", hd->timeline_next);
472 layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd,
473 "drm_hwc_release", hd->timeline_next);
Allen Martin3d3f70a2015-02-21 21:20:17 -0800474 hd->buf_queue.push_back(buf);
Sean Paul9b1bb842015-01-23 01:11:58 -0500475
Sean Paul9aa5ad32015-01-22 15:47:54 -0500476 ret = pthread_cond_signal(&hd->set_worker.cond);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200477 if (ret)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500478 ALOGE("Failed to signal set worker %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500479
Lauri Peltonen132e0102015-02-12 13:54:33 +0200480 if (pthread_mutex_unlock(&hd->set_worker.lock))
481 ALOGE("Failed to unlock set lock in set()");
Sean Paul9aa5ad32015-01-22 15:47:54 -0500482
Sean Paule0c4c3d2015-01-20 16:56:04 -0500483out:
Lauri Peltonen132e0102015-02-12 13:54:33 +0200484 /* Close input fences. */
485 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
486 layer = &display_contents->hwLayers[i];
487 if (layer->acquireFenceFd >= 0) {
488 close(layer->acquireFenceFd);
489 layer->acquireFenceFd = -1;
490 }
491 }
492 if (display_contents->outbufAcquireFenceFd >= 0) {
493 close(display_contents->outbufAcquireFenceFd);
494 display_contents->outbufAcquireFenceFd = -1;
495 }
Sean Paul9aa5ad32015-01-22 15:47:54 -0500496
Sean Paule0c4c3d2015-01-20 16:56:04 -0500497 return ret;
498}
499
500static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
501 hwc_display_contents_1_t** display_contents)
502{
503 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
504 int ret = 0, i;
505
Sean Paule0c4c3d2015-01-20 16:56:04 -0500506 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
Sean Pauldffca952015-02-04 10:19:55 -0800507 if (display_contents[i])
508 ret = hwc_set_display(ctx, i, display_contents[i]);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500509 }
510
511 return ret;
512}
513
Sean Pauleb9e75c2015-01-25 23:31:30 -0500514static int hwc_wait_for_vsync(struct hwc_drm_display *hd)
Sean Paule0c4c3d2015-01-20 16:56:04 -0500515{
Sean Pauleb9e75c2015-01-25 23:31:30 -0500516 drmVBlank vblank;
517 int ret;
518 uint32_t high_crtc;
519 int64_t timestamp;
520
521 if (hd->active_pipe == -1)
522 return -EINVAL;
523
524 memset(&vblank, 0, sizeof(vblank));
525
526 high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
527 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
528 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
529 vblank.request.sequence = 1;
530
531 ret = drmWaitVBlank(hd->ctx->fd, &vblank);
532 if (ret) {
533 ALOGE("Failed to wait for vblank %d", ret);
534 return ret;
535 }
536
537 if (hd->ctx->procs->vsync) {
538 timestamp = vblank.reply.tval_sec * 1000 * 1000 * 1000 +
539 vblank.reply.tval_usec * 1000;
540 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
541 }
542
543 return 0;
544}
545
546static void *hwc_vsync_worker(void *arg)
547{
548 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
549 struct hwc_worker *w = &hd->vsync_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500550 int ret;
551
Sean Pauleb9e75c2015-01-25 23:31:30 -0500552 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
553
554 do {
555 ret = pthread_mutex_lock(&w->lock);
556 if (ret) {
557 ALOGE("Failed to lock vsync lock %d", ret);
558 return NULL;
559 }
560
561 if (hd->active_pipe == -1) {
562 ALOGE("Pipe is no longer active, disable events");
563 hd->enable_vsync_events = false;
564 }
565
566 if (!hd->enable_vsync_events) {
567 ret = pthread_cond_wait(&w->cond, &w->lock);
568 if (ret) {
569 ALOGE("Failed to wait on vsync cond %d", ret);
570 break;
571 }
572 }
573
574 if (w->exit)
575 break;
576
577 ret = pthread_mutex_unlock(&w->lock);
578 if (ret) {
579 ALOGE("Failed to unlock vsync lock %d", ret);
580 return NULL;
581 }
582
583 if (!hd->enable_vsync_events)
584 continue;
585
586 ret = hwc_wait_for_vsync(hd);
587 if (ret)
588 ALOGE("Failed to wait for vsync %d", ret);
589
590 } while (true);
591
592out:
593 ret = pthread_mutex_unlock(&hd->set_worker.lock);
594 if (ret)
595 ALOGE("Failed to unlock set lock while exiting %d", ret);
596
597 return NULL;
598}
599
600static int hwc_event_control(struct hwc_composer_device_1* dev, int display,
601 int event, int enabled)
602{
603 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
604 struct hwc_drm_display *hd = NULL;
605 int ret;
606
607 ret = hwc_get_drm_display(ctx, display, &hd);
608 if (ret)
609 return ret;
610
611 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
612 return -EINVAL;
613
614 if (hd->active_pipe == -1) {
615 ALOGD("Can't service events for display %d, no pipe", display);
616 return -EINVAL;
617 }
618
619 ret = pthread_mutex_lock(&hd->vsync_worker.lock);
620 if (ret) {
621 ALOGE("Failed to lock vsync lock %d", ret);
622 return ret;
623 }
624
625 hd->enable_vsync_events = !!enabled;
626
627 ret = pthread_cond_signal(&hd->vsync_worker.cond);
628 if (ret) {
629 ALOGE("Failed to signal vsync thread %d", ret);
630 goto out;
631 }
632
633 ret = pthread_mutex_unlock(&hd->vsync_worker.lock);
634 if (ret) {
635 ALOGE("Failed to unlock vsync lock %d", ret);
636 return ret;
637 }
638
Sean Paule0c4c3d2015-01-20 16:56:04 -0500639 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500640
641out:
642 if (pthread_mutex_unlock(&hd->vsync_worker.lock))
643 ALOGE("Failed to unlock vsync worker in error path");
644
645 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500646}
647
648static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display,
649 int mode)
650{
651 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
652 struct hwc_drm_display *hd = NULL;
653 drmModeConnectorPtr c;
654 int ret, i;
655 uint32_t dpms_prop = 0;
656 uint64_t dpms_value = 0;
657
658 ret = hwc_get_drm_display(ctx, display, &hd);
659 if (ret)
660 return ret;
661
662 c = drmModeGetConnector(ctx->fd, hd->connector_id);
663 if (!c) {
664 ALOGE("Failed to get connector %d", display);
665 return -ENODEV;
666 }
667
668 for (i = 0; !dpms_prop && i < c->count_props; i++) {
669 drmModePropertyPtr p;
670
671 p = drmModeGetProperty(ctx->fd, c->props[i]);
672 if (!p)
673 continue;
674
675 if (!strcmp(p->name, "DPMS"))
676 dpms_prop = c->props[i];
677
678 drmModeFreeProperty(p);
679 }
680 if (!dpms_prop) {
681 ALOGE("Failed to get DPMS property from display %d", display);
682 ret = -ENOENT;
683 goto out;
684 }
685
686 switch(mode) {
687 case HWC_POWER_MODE_OFF:
688 dpms_value = DRM_MODE_DPMS_OFF;
689 break;
690
691 /* We can't support dozing right now, so go full on */
692 case HWC_POWER_MODE_DOZE:
693 case HWC_POWER_MODE_DOZE_SUSPEND:
694 case HWC_POWER_MODE_NORMAL:
695 dpms_value = DRM_MODE_DPMS_ON;
696 break;
697 };
698
699 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id,
700 dpms_prop, dpms_value);
701 if (ret) {
702 ALOGE("Failed to set DPMS property for display %d", display);
703 goto out;
704 }
705
706out:
707 drmModeFreeConnector(c);
708 return ret;
709}
710
711static int hwc_query(struct hwc_composer_device_1 */* dev */, int what,
712 int *value)
713{
714 switch(what) {
715 case HWC_BACKGROUND_LAYER_SUPPORTED:
716 *value = 0; /* TODO: We should do this */
717 break;
718 case HWC_VSYNC_PERIOD:
719 ALOGW("Query for deprecated vsync value, returning 60Hz");
720 *value = 1000 * 1000 * 1000 / 60;
721 break;
722 case HWC_DISPLAY_TYPES_SUPPORTED:
723 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
724 break;
725 }
726 return 0;
727}
728
729static void hwc_register_procs(struct hwc_composer_device_1* dev,
730 hwc_procs_t const* procs)
731{
732 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
733
734 ctx->procs = procs;
735}
736
737static int hwc_get_display_configs(struct hwc_composer_device_1* dev,
738 int display, uint32_t* configs, size_t* numConfigs)
739{
740 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
741 struct hwc_drm_display *hd = NULL;
742 drmModeConnectorPtr c;
743 int ret = 0, i;
744
745 if (!*numConfigs)
746 return 0;
747
748 ret = hwc_get_drm_display(ctx, display, &hd);
749 if (ret)
750 return ret;
751
752 c = drmModeGetConnector(ctx->fd, hd->connector_id);
753 if (!c) {
754 ALOGE("Failed to get connector %d", display);
755 return -ENODEV;
756 }
757
Sean Paula4283c52015-02-04 10:08:00 -0800758 if (hd->configs) {
Sean Paule0c4c3d2015-01-20 16:56:04 -0500759 free(hd->configs);
Sean Paula4283c52015-02-04 10:08:00 -0800760 hd->configs = NULL;
761 }
762
763 if (c->connection == DRM_MODE_DISCONNECTED) {
764 ret = -ENODEV;
765 goto out;
766 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500767
Sean Paule0c4c3d2015-01-20 16:56:04 -0500768 hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes,
769 sizeof(*hd->configs));
770 if (!hd->configs) {
771 ALOGE("Failed to allocate config list for display %d", display);
772 ret = -ENOMEM;
773 hd->num_configs = 0;
774 goto out;
775 }
776
777 for (i = 0; i < c->count_modes; i++) {
778 drmModeModeInfoPtr m = &hd->configs[i];
779
780 memcpy(m, &c->modes[i], sizeof(*m));
781
782 if (i < (int)*numConfigs)
783 configs[i] = i;
784 }
785
786 hd->num_configs = c->count_modes;
787 *numConfigs = MIN(c->count_modes, *numConfigs);
788
789out:
790 drmModeFreeConnector(c);
791 return ret;
792}
793
794static int hwc_check_config_valid(struct hwc_context_t *ctx,
795 drmModeConnectorPtr connector, int display,
796 int config_idx)
797{
798 struct hwc_drm_display *hd = NULL;
799 drmModeModeInfoPtr m = NULL;
800 int ret = 0, i;
801
802 ret = hwc_get_drm_display(ctx, display, &hd);
803 if (ret)
804 return ret;
805
806 /* Make sure the requested config is still valid for the display */
807 for (i = 0; i < connector->count_modes; i++) {
808 if (hwc_mode_is_equal(&connector->modes[i],
809 &hd->configs[config_idx])) {
810 m = &hd->configs[config_idx];
811 break;
812 }
813 }
814 if (!m)
815 return -ENOENT;
816
817 return 0;
818}
819
820static int hwc_get_display_attributes(struct hwc_composer_device_1* dev,
821 int display, uint32_t config, const uint32_t* attributes,
822 int32_t* values)
823{
824 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
825 struct hwc_drm_display *hd = NULL;
826 drmModeConnectorPtr c;
827 drmModeModeInfoPtr m;
828 int ret, i;
829
830 ret = hwc_get_drm_display(ctx, display, &hd);
831 if (ret)
832 return ret;
833
834 if (config >= hd->num_configs) {
835 ALOGE("Requested config is out-of-bounds %d %d", config,
836 hd->num_configs);
837 return -EINVAL;
838 }
839
840 c = drmModeGetConnector(ctx->fd, hd->connector_id);
841 if (!c) {
842 ALOGE("Failed to get connector %d", display);
843 return -ENODEV;
844 }
845
846 ret = hwc_check_config_valid(ctx, c, display, (int)config);
847 if (ret) {
848 ALOGE("Provided config is no longer valid %u", config);
849 goto out;
850 }
851
852 m = &hd->configs[config];
853 for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
854 switch(attributes[i]) {
855 case HWC_DISPLAY_VSYNC_PERIOD:
856 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
857 break;
858 case HWC_DISPLAY_WIDTH:
859 values[i] = m->hdisplay;
860 break;
861 case HWC_DISPLAY_HEIGHT:
862 values[i] = m->vdisplay;
863 break;
864 case HWC_DISPLAY_DPI_X:
865 /* Dots per 1000 inches */
866 values[i] = c->mmWidth ?
867 (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
868 break;
869 case HWC_DISPLAY_DPI_Y:
870 /* Dots per 1000 inches */
871 values[i] = c->mmHeight ?
872 (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
873 break;
874 }
875 }
876
877out:
878 drmModeFreeConnector(c);
879 return ret;
880}
881
882static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display)
883{
884 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
885 struct hwc_drm_display *hd = NULL;
Sean Paulfa406a12015-02-04 10:05:44 -0800886 int ret, i, index = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500887
888 ret = hwc_get_drm_display(ctx, display, &hd);
889 if (ret)
890 return ret;
891
Sean Paulfa406a12015-02-04 10:05:44 -0800892 /* Find the current mode in the config list */
893 for (i = 0; i < (int)hd->num_configs; i++) {
894 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
895 index = i;
896 break;
897 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500898 }
899
Sean Paulfa406a12015-02-04 10:05:44 -0800900 return index;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500901}
902
903static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id)
904{
905 int i;
906
907 for (i = 0; i < MAX_NUM_DISPLAYS; i++) {
908 if (ctx->displays[i].active_crtc == crtc_id)
909 return true;
910 }
911 return false;
912}
913
914static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
915 uint32_t encoder_id, uint32_t *crtc_id)
916{
917 drmModeEncoderPtr e;
918 int ret, i;
919
920 e = drmModeGetEncoder(ctx->fd, encoder_id);
921 if (!e) {
922 ALOGE("Failed to get encoder for connector %d", encoder_id);
923 return -ENODEV;
924 }
925
926 /* First try to use the currently-bound crtc */
927 if (e->crtc_id) {
928 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
929 *crtc_id = e->crtc_id;
930 ret = 0;
931 goto out;
932 }
933 }
934
935 /* Try to find a possible crtc which will work */
936 for (i = 0; i < r->count_crtcs; i++) {
937 if (!(e->possible_crtcs & (1 << i)))
938 continue;
939
940 /* We've already tried this earlier */
941 if (e->crtc_id == r->crtcs[i])
942 continue;
943
944 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
945 *crtc_id = r->crtcs[i];
946 ret = 0;
947 goto out;
948 }
949 }
950
951 /* We can't use the encoder, but nothing went wrong, try another one */
952 ret = -EAGAIN;
953
954out:
955 drmModeFreeEncoder(e);
956 return ret;
957}
958
959static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display,
960 int index)
961{
962 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
963 struct hwc_drm_display *hd = NULL;
964 drmModeResPtr r = NULL;
965 drmModeConnectorPtr c;
966 uint32_t crtc_id = 0;
967 int ret, i;
968 bool new_crtc, new_encoder;
969
970 ret = hwc_get_drm_display(ctx, display, &hd);
971 if (ret)
972 return ret;
973
974 c = drmModeGetConnector(ctx->fd, hd->connector_id);
975 if (!c) {
976 ALOGE("Failed to get connector %d", display);
977 return -ENODEV;
978 }
979
980 if (c->connection == DRM_MODE_DISCONNECTED) {
981 ALOGE("Tried to configure a disconnected display %d", display);
982 ret = -ENODEV;
983 goto out;
984 }
985
Sean Paulfa406a12015-02-04 10:05:44 -0800986 if (index >= c->count_modes) {
987 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500988 ret = -ENOENT;
989 goto out;
990 }
991
992 r = drmModeGetResources(ctx->fd);
993 if (!r) {
994 ALOGE("Failed to get drm resources");
995 goto out;
996 }
997
998 /* We no longer have an active_crtc */
999 hd->active_crtc = 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001000 hd->active_pipe = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001001
1002 /* First, try to use the currently-connected encoder */
1003 if (c->encoder_id) {
1004 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
1005 if (ret && ret != -EAGAIN) {
1006 ALOGE("Encoder try failed %d", ret);
1007 goto out;
1008 }
1009 }
1010
1011 /* We couldn't find a crtc with the attached encoder, try the others */
1012 if (!crtc_id) {
1013 for (i = 0; i < c->count_encoders; i++) {
1014 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
1015 if (!ret) {
1016 break;
1017 } else if (ret != -EAGAIN) {
1018 ALOGE("Encoder try failed %d", ret);
1019 goto out;
1020 }
1021 }
1022 if (!crtc_id) {
1023 ALOGE("Couldn't find valid crtc to modeset");
1024 ret = -EINVAL;
1025 goto out;
1026 }
1027 }
1028
1029 hd->active_crtc = crtc_id;
Sean Paulfa406a12015-02-04 10:05:44 -08001030
1031 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
Sean Paule0c4c3d2015-01-20 16:56:04 -05001032
Sean Pauleb9e75c2015-01-25 23:31:30 -05001033 /* Find the pipe corresponding to the crtc_id */
1034 for (i = 0; i < r->count_crtcs; i++) {
1035 /* We've already tried this earlier */
1036 if (r->crtcs[i] == crtc_id) {
1037 hd->active_pipe = i;
1038 break;
1039 }
1040 }
1041 /* This should never happen... hehehe */
1042 if (hd->active_pipe == -1) {
1043 ALOGE("Active crtc was not found in resources!!");
1044 ret = -ENODEV;
1045 goto out;
1046 }
1047
Sean Paule0c4c3d2015-01-20 16:56:04 -05001048 /* TODO: Once we have atomic, set the crtc timing info here */
1049
1050out:
1051 if (r)
1052 drmModeFreeResources(r);
1053
1054 drmModeFreeConnector(c);
1055 return ret;
1056}
1057
Sean Paul9aa5ad32015-01-22 15:47:54 -05001058static int hwc_destroy_worker(struct hwc_worker *worker)
1059{
1060 int ret;
1061
1062 ret = pthread_mutex_lock(&worker->lock);
1063 if (ret) {
1064 ALOGE("Failed to lock in destroy() %d", ret);
1065 return ret;
1066 }
1067
1068 worker->exit = true;
1069
1070 ret |= pthread_cond_signal(&worker->cond);
1071 if (ret)
1072 ALOGE("Failed to signal cond in destroy() %d", ret);
1073
1074 ret |= pthread_mutex_unlock(&worker->lock);
1075 if (ret)
1076 ALOGE("Failed to unlock in destroy() %d", ret);
1077
1078 ret |= pthread_join(worker->thread, NULL);
1079 if (ret && ret != ESRCH)
1080 ALOGE("Failed to join thread in destroy() %d", ret);
1081
1082 return ret;
1083}
1084
1085static void hwc_destroy_display(struct hwc_drm_display *hd)
1086{
1087 int ret;
1088
1089 if (hwc_destroy_worker(&hd->set_worker))
1090 ALOGE("Destroy set worker failed");
Sean Pauleb9e75c2015-01-25 23:31:30 -05001091
1092 if (hwc_destroy_worker(&hd->vsync_worker))
1093 ALOGE("Destroy vsync worker failed");
Sean Paul9aa5ad32015-01-22 15:47:54 -05001094}
1095
Sean Paule0c4c3d2015-01-20 16:56:04 -05001096static int hwc_device_close(struct hw_device_t *dev)
1097{
1098 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulcd36a9e2015-01-22 18:01:18 -05001099 int ret, i;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001100
Sean Paul9aa5ad32015-01-22 15:47:54 -05001101 for (i = 0; i < MAX_NUM_DISPLAYS; i++)
1102 hwc_destroy_display(&ctx->displays[i]);
1103
1104 drmClose(ctx->fd);
Sean Paulcd36a9e2015-01-22 18:01:18 -05001105
1106 ret = hwc_import_destroy(ctx->import_ctx);
1107 if (ret)
1108 ALOGE("Could not destroy import %d", ret);
1109
Sean Paule0c4c3d2015-01-20 16:56:04 -05001110 free(ctx);
1111
1112 return 0;
1113}
1114
Sean Paul9aa5ad32015-01-22 15:47:54 -05001115static int hwc_initialize_worker(struct hwc_drm_display *hd,
1116 struct hwc_worker *worker, void *(*routine)(void*))
1117{
1118 int ret;
1119
1120 ret = pthread_cond_init(&worker->cond, NULL);
1121 if (ret) {
1122 ALOGE("Failed to create worker condition %d", ret);
1123 return ret;
1124 }
1125
1126 ret = pthread_mutex_init(&worker->lock, NULL);
1127 if (ret) {
1128 ALOGE("Failed to initialize worker lock %d", ret);
1129 goto err_cond;
1130 }
1131
1132 worker->exit = false;
1133
1134 ret = pthread_create(&worker->thread, NULL, routine, hd);
1135 if (ret) {
1136 ALOGE("Could not create worker thread %d", ret);
1137 goto err_lock;
1138 }
1139 return 0;
1140
1141err_lock:
1142 pthread_mutex_destroy(&worker->lock);
1143err_cond:
1144 pthread_cond_destroy(&worker->cond);
1145 return ret;
1146}
1147
Sean Paul24a26e32015-02-04 10:34:47 -08001148/*
1149 * TODO: This function sets the active config to the first one in the list. This
1150 * should be fixed such that it selects the preferred mode for the display, or
1151 * some other, saner, method of choosing the config.
1152 */
1153static int hwc_set_initial_config(struct hwc_drm_display *hd)
1154{
1155 int ret;
1156 uint32_t config;
1157 size_t num_configs = 1;
1158
1159 ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1160 &num_configs);
Sean Paulaea15c22015-02-09 02:24:11 -05001161 if (ret || !num_configs)
Sean Paul24a26e32015-02-04 10:34:47 -08001162 return 0;
1163
1164 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1165 if (ret) {
1166 ALOGE("Failed to set active config d=%d ret=%d", hd->display,
1167 ret);
1168 return ret;
1169 }
1170
1171 return ret;
1172}
1173
Sean Paule0c4c3d2015-01-20 16:56:04 -05001174static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1175 uint32_t connector_id)
1176{
1177 struct hwc_drm_display *hd = NULL;
1178 int ret;
1179
1180 ret = hwc_get_drm_display(ctx, display, &hd);
1181 if (ret)
1182 return ret;
1183
Sean Paul9aa5ad32015-01-22 15:47:54 -05001184 hd->ctx = ctx;
1185 hd->display = display;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001186 hd->active_pipe = -1;
Sean Paulefb20cb2015-02-04 09:29:15 -08001187 hd->initial_modeset_required = true;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001188 hd->connector_id = connector_id;
1189
Sean Paulf1dc1912015-01-24 01:34:31 -05001190 ret = sw_sync_timeline_create();
1191 if (ret < 0) {
1192 ALOGE("Failed to create sw sync timeline %d", ret);
1193 return ret;
1194 }
1195 hd->timeline_fd = ret;
1196
Sean Paul24a26e32015-02-04 10:34:47 -08001197 ret = hwc_set_initial_config(hd);
1198 if (ret) {
1199 ALOGE("Failed to set initial config for d=%d ret=%d", display,
1200 ret);
1201 return ret;
1202 }
1203
Sean Paul9aa5ad32015-01-22 15:47:54 -05001204 ret = hwc_initialize_worker(hd, &hd->set_worker, hwc_set_worker);
1205 if (ret) {
1206 ALOGE("Failed to create set worker %d\n", ret);
1207 return ret;
1208 }
1209
Sean Pauleb9e75c2015-01-25 23:31:30 -05001210 ret = hwc_initialize_worker(hd, &hd->vsync_worker, hwc_vsync_worker);
1211 if (ret) {
1212 ALOGE("Failed to create vsync worker %d", ret);
1213 goto err;
1214 }
1215
Sean Paule0c4c3d2015-01-20 16:56:04 -05001216 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001217
1218err:
1219 if (hwc_destroy_worker(&hd->set_worker))
1220 ALOGE("Failed to destroy set worker");
1221
1222 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001223}
1224
1225static int hwc_enumerate_displays(struct hwc_context_t *ctx)
1226{
1227 struct hwc_drm_display *panel_hd;
1228 drmModeResPtr res;
1229 drmModeConnectorPtr *conn_list;
1230 int ret = 0, i, j;
1231
1232 res = drmModeGetResources(ctx->fd);
1233 if (!res) {
1234 ALOGE("Failed to get drm resources");
1235 return -ENODEV;
1236 }
1237
1238 conn_list = (drmModeConnector **)calloc(res->count_connectors,
1239 sizeof(*conn_list));
1240 if (!conn_list) {
1241 ALOGE("Failed to allocate connector list");
1242 ret = -ENOMEM;
1243 goto out;
1244 }
1245
1246 for (i = 0; i < res->count_connectors; i++) {
1247 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1248 if (!conn_list[i]) {
1249 ALOGE("Failed to get connector %d", res->connectors[i]);
1250 ret = -ENODEV;
1251 goto out;
1252 }
1253 }
1254
1255 ctx->num_displays = 0;
1256
1257 /* Find a connected, panel type connector for display 0 */
1258 for (i = 0; i < res->count_connectors; i++) {
1259 drmModeConnectorPtr c = conn_list[i];
1260
1261 for (j = 0; j < ARRAY_SIZE(panel_types); j++) {
1262 if (c->connector_type == panel_types[j] &&
1263 c->connection == DRM_MODE_CONNECTED)
1264 break;
1265 }
1266 if (j == ARRAY_SIZE(panel_types))
1267 continue;
1268
1269 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1270 ctx->num_displays++;
1271 break;
1272 }
1273
1274 ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1275 if (ret)
1276 goto out;
1277
1278 /* Fill in the other displays */
1279 for (i = 0; i < res->count_connectors; i++) {
1280 drmModeConnectorPtr c = conn_list[i];
1281
1282 if (panel_hd->connector_id == c->connector_id)
1283 continue;
1284
1285 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1286 ctx->num_displays++;
1287 }
1288
1289out:
1290 for (i = 0; i < res->count_connectors; i++) {
1291 if (conn_list[i])
1292 drmModeFreeConnector(conn_list[i]);
1293 }
1294 free(conn_list);
1295
1296 if (res)
1297 drmModeFreeResources(res);
1298
1299 return ret;
1300}
1301
1302static int hwc_device_open(const struct hw_module_t* module, const char* name,
1303 struct hw_device_t** dev)
1304{
1305 int ret = 0;
1306 struct hwc_context_t *ctx;
Lauri Peltonen64717b22015-02-04 16:55:31 +02001307 char path[PROPERTY_VALUE_MAX];
Sean Paule0c4c3d2015-01-20 16:56:04 -05001308
1309 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1310 ALOGE("Invalid module name- %s", name);
1311 return -EINVAL;
1312 }
1313
Sean Paul9b1bb842015-01-23 01:11:58 -05001314 ctx = new hwc_context_t();
Sean Paule0c4c3d2015-01-20 16:56:04 -05001315 if (!ctx) {
1316 ALOGE("Failed to allocate hwc context");
1317 return -ENOMEM;
1318 }
1319
Sean Paulcd36a9e2015-01-22 18:01:18 -05001320 ret = hwc_import_init(&ctx->import_ctx);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001321 if (ret) {
Sean Paulcd36a9e2015-01-22 18:01:18 -05001322 ALOGE("Failed to initialize import context");
Sean Paule0c4c3d2015-01-20 16:56:04 -05001323 goto out;
1324 }
1325
Lauri Peltonen64717b22015-02-04 16:55:31 +02001326 property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001327 /* TODO: Use drmOpenControl here instead */
Lauri Peltonen64717b22015-02-04 16:55:31 +02001328 ctx->fd = open(path, O_RDWR);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001329 if (ctx->fd < 0) {
1330 ALOGE("Failed to open dri- %s", strerror(-errno));
1331 goto out;
1332 }
1333
Sean Paule0c4c3d2015-01-20 16:56:04 -05001334 ret = hwc_enumerate_displays(ctx);
1335 if (ret) {
1336 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1337 goto out;
1338 }
1339
1340 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1341 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1342 ctx->device.common.module = const_cast<hw_module_t*>(module);
1343 ctx->device.common.close = hwc_device_close;
1344
1345 ctx->device.prepare = hwc_prepare;
1346 ctx->device.set = hwc_set;
1347 ctx->device.eventControl = hwc_event_control;
1348 ctx->device.setPowerMode = hwc_set_power_mode;
1349 ctx->device.query = hwc_query;
1350 ctx->device.registerProcs = hwc_register_procs;
1351 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1352 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1353 ctx->device.getActiveConfig = hwc_get_active_config;
1354 ctx->device.setActiveConfig = hwc_set_active_config;
1355 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1356
1357 *dev = &ctx->device.common;
1358
1359 return 0;
1360out:
1361 if (ctx->fd >= 0)
1362 close(ctx->fd);
1363
1364 free(ctx);
1365 return ret;
1366}
1367
1368static struct hw_module_methods_t hwc_module_methods = {
1369 open: hwc_device_open
1370};
1371
1372hwc_module_t HAL_MODULE_INFO_SYM = {
1373 common: {
1374 tag: HARDWARE_MODULE_TAG,
1375 version_major: 1,
1376 version_minor: 0,
1377 id: HWC_HARDWARE_MODULE_ID,
1378 name: "DRM hwcomposer module",
1379 author: "The Android Open Source Project",
1380 methods: &hwc_module_methods,
1381 dso: NULL,
1382 reserved: { 0 },
1383 }
1384};