blob: 7a4b3023aa4ca244d031c980ddc13663856c7835 [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 Paul3bc48e82015-01-23 01:41:13 -0500293 ret = hwc_flip(hd, buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500294 if (ret) {
295 ALOGE("Failed to perform flip\n");
296 return ret;
297 }
298
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200299 if (hwc_import_bo_release(hd->ctx->fd, hd->ctx->import_ctx, &hd->front)) {
300 memset(&args, 0, sizeof(args));
301 for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
302 if (!hd->front.gem_handles[i])
303 continue;
Allen Martin3d3f70a2015-02-21 21:20:17 -0800304
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200305 /* check for duplicate handle in buf_queue */
306 bool found;
Allen Martin3d3f70a2015-02-21 21:20:17 -0800307
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200308 ret = pthread_mutex_lock(&hd->set_worker.lock);
309 if (ret) {
310 ALOGE("Failed to lock set lock in wait_and_set() %d", ret);
311 continue;
312 }
Allen Martin3d3f70a2015-02-21 21:20:17 -0800313
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200314 found = false;
315 for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin();
316 bi != hd->buf_queue.end();
317 ++bi)
318 for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++)
319 if (hd->front.gem_handles[i] == bi->gem_handles[j] )
320 found = true;
321
322 for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); j++)
323 if (hd->front.gem_handles[i] == buf->gem_handles[j])
Allen Martin3d3f70a2015-02-21 21:20:17 -0800324 found = true;
325
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200326 if (!found) {
327 args.handle = hd->front.gem_handles[i];
328 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
329 }
330 if (pthread_mutex_unlock(&hd->set_worker.lock))
331 ALOGE("Failed to unlock set lock in wait_and_set() %d", ret);
Allen Martin91fa3f82015-02-22 04:48:14 -0800332 }
333 }
334
Sean Paul3bc48e82015-01-23 01:41:13 -0500335 hd->front = *buf;
336
Sean Paul9aa5ad32015-01-22 15:47:54 -0500337 return ret;
338}
339
340static void *hwc_set_worker(void *arg)
341{
342 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
343 int ret;
344
345 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
346
Sean Paul9aa5ad32015-01-22 15:47:54 -0500347 do {
Sean Paul3bc48e82015-01-23 01:41:13 -0500348 struct hwc_drm_bo buf;
349
350 ret = pthread_mutex_lock(&hd->set_worker.lock);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500351 if (ret) {
Sean Paul3bc48e82015-01-23 01:41:13 -0500352 ALOGE("Failed to lock set lock %d", ret);
353 return NULL;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500354 }
355
Sean Paul3bc48e82015-01-23 01:41:13 -0500356 if (hd->set_worker.exit)
357 goto out;
358
359 if (hd->buf_queue.empty()) {
360 ret = pthread_cond_wait(&hd->set_worker.cond,
361 &hd->set_worker.lock);
362 if (ret) {
363 ALOGE("Failed to wait on condition %d", ret);
364 goto out;
365 }
366 }
367
368 buf = hd->buf_queue.front();
Allen Martin3d3f70a2015-02-21 21:20:17 -0800369 hd->buf_queue.pop_front();
Sean Paul3bc48e82015-01-23 01:41:13 -0500370
371 ret = pthread_mutex_unlock(&hd->set_worker.lock);
372 if (ret) {
373 ALOGE("Failed to unlock set lock %d", ret);
374 return NULL;
375 }
376
377 ret = hwc_wait_and_set(hd, &buf);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500378 if (ret)
379 ALOGE("Failed to wait and set %d", ret);
Sean Paulf1dc1912015-01-24 01:34:31 -0500380
381 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
382 if (ret)
383 ALOGE("Failed to increment sync timeline %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500384 } while (true);
385
Sean Paul3bc48e82015-01-23 01:41:13 -0500386out:
Sean Paul9aa5ad32015-01-22 15:47:54 -0500387 ret = pthread_mutex_unlock(&hd->set_worker.lock);
Sean Paul3bc48e82015-01-23 01:41:13 -0500388 if (ret)
389 ALOGE("Failed to unlock set lock while exiting %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500390
391 return NULL;
392}
393
Sean Paule0c4c3d2015-01-20 16:56:04 -0500394static int hwc_set_display(hwc_context_t *ctx, int display,
395 hwc_display_contents_1_t* display_contents)
396{
397 struct hwc_drm_display *hd = NULL;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500398 hwc_layer_1_t *layer = NULL;
Sean Paul9b1bb842015-01-23 01:11:58 -0500399 struct hwc_drm_bo buf;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500400 int ret, i;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500401 uint32_t fb_id;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500402
Sean Paul9b1bb842015-01-23 01:11:58 -0500403 memset(&buf, 0, sizeof(buf));
404
Sean Paule0c4c3d2015-01-20 16:56:04 -0500405 ret = hwc_get_drm_display(ctx, display, &hd);
406 if (ret)
Lauri Peltonen132e0102015-02-12 13:54:33 +0200407 goto out;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500408
409 if (!hd->active_crtc) {
410 ALOGE("There is no active crtc for display %d", display);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200411 ret = -ENOENT;
412 goto out;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500413 }
414
415 /*
416 * TODO: We can only support one hw layer atm, so choose either the
417 * first one or the framebuffer target.
418 */
419 if (!display_contents->numHwLayers) {
420 return 0;
421 } else if (display_contents->numHwLayers == 1) {
422 layer = &display_contents->hwLayers[0];
423 } else {
424 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
425 layer = &display_contents->hwLayers[i];
426 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
427 break;
428 }
429 if (i == (int)display_contents->numHwLayers) {
430 ALOGE("Could not find a suitable layer for display %d",
431 display);
432 }
433 }
434
Allen Martin3d3f70a2015-02-21 21:20:17 -0800435
436 ret = pthread_mutex_lock(&hd->set_worker.lock);
437 if (ret) {
438 ALOGE("Failed to lock set lock in set() %d", ret);
439 goto out;
440 }
441
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200442 ret = hwc_import_bo_create(ctx->fd, ctx->import_ctx, layer->handle,
Sean Paul3bc48e82015-01-23 01:41:13 -0500443 &buf);
444 if (ret) {
445 ALOGE("Failed to import handle to drm bo %d", ret);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200446 goto out;
Sean Paul3bc48e82015-01-23 01:41:13 -0500447 }
448 buf.acquire_fence_fd = layer->acquireFenceFd;
Lauri Peltonen132e0102015-02-12 13:54:33 +0200449 layer->acquireFenceFd = -1;
Sean Paul3bc48e82015-01-23 01:41:13 -0500450
Sean Paulf1dc1912015-01-24 01:34:31 -0500451 /*
452 * TODO: Retire and release can use the same sync point here b/c hwc is
453 * restricted to one layer. Once that is no longer true, this will need
454 * to change
455 */
456 hd->timeline_next++;
457 display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd,
458 "drm_hwc_retire", hd->timeline_next);
459 layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd,
460 "drm_hwc_release", hd->timeline_next);
Allen Martin3d3f70a2015-02-21 21:20:17 -0800461 hd->buf_queue.push_back(buf);
Sean Paul9b1bb842015-01-23 01:11:58 -0500462
Sean Paul9aa5ad32015-01-22 15:47:54 -0500463 ret = pthread_cond_signal(&hd->set_worker.cond);
Lauri Peltonen132e0102015-02-12 13:54:33 +0200464 if (ret)
Sean Paul9aa5ad32015-01-22 15:47:54 -0500465 ALOGE("Failed to signal set worker %d", ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500466
Lauri Peltonen132e0102015-02-12 13:54:33 +0200467 if (pthread_mutex_unlock(&hd->set_worker.lock))
468 ALOGE("Failed to unlock set lock in set()");
Sean Paul9aa5ad32015-01-22 15:47:54 -0500469
Sean Paule0c4c3d2015-01-20 16:56:04 -0500470out:
Lauri Peltonen132e0102015-02-12 13:54:33 +0200471 /* Close input fences. */
472 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
473 layer = &display_contents->hwLayers[i];
474 if (layer->acquireFenceFd >= 0) {
475 close(layer->acquireFenceFd);
476 layer->acquireFenceFd = -1;
477 }
478 }
479 if (display_contents->outbufAcquireFenceFd >= 0) {
480 close(display_contents->outbufAcquireFenceFd);
481 display_contents->outbufAcquireFenceFd = -1;
482 }
Sean Paul9aa5ad32015-01-22 15:47:54 -0500483
Sean Paule0c4c3d2015-01-20 16:56:04 -0500484 return ret;
485}
486
487static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
488 hwc_display_contents_1_t** display_contents)
489{
490 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
491 int ret = 0, i;
492
Sean Paule0c4c3d2015-01-20 16:56:04 -0500493 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
Sean Pauldffca952015-02-04 10:19:55 -0800494 if (display_contents[i])
495 ret = hwc_set_display(ctx, i, display_contents[i]);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500496 }
497
498 return ret;
499}
500
Sean Pauleb9e75c2015-01-25 23:31:30 -0500501static int hwc_wait_for_vsync(struct hwc_drm_display *hd)
Sean Paule0c4c3d2015-01-20 16:56:04 -0500502{
Sean Pauleb9e75c2015-01-25 23:31:30 -0500503 drmVBlank vblank;
504 int ret;
505 uint32_t high_crtc;
506 int64_t timestamp;
507
508 if (hd->active_pipe == -1)
509 return -EINVAL;
510
511 memset(&vblank, 0, sizeof(vblank));
512
513 high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
514 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
515 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
516 vblank.request.sequence = 1;
517
518 ret = drmWaitVBlank(hd->ctx->fd, &vblank);
519 if (ret) {
520 ALOGE("Failed to wait for vblank %d", ret);
521 return ret;
522 }
523
524 if (hd->ctx->procs->vsync) {
525 timestamp = vblank.reply.tval_sec * 1000 * 1000 * 1000 +
526 vblank.reply.tval_usec * 1000;
527 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
528 }
529
530 return 0;
531}
532
533static void *hwc_vsync_worker(void *arg)
534{
535 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
536 struct hwc_worker *w = &hd->vsync_worker;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500537 int ret;
538
Sean Pauleb9e75c2015-01-25 23:31:30 -0500539 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
540
541 do {
542 ret = pthread_mutex_lock(&w->lock);
543 if (ret) {
544 ALOGE("Failed to lock vsync lock %d", ret);
545 return NULL;
546 }
547
548 if (hd->active_pipe == -1) {
549 ALOGE("Pipe is no longer active, disable events");
550 hd->enable_vsync_events = false;
551 }
552
553 if (!hd->enable_vsync_events) {
554 ret = pthread_cond_wait(&w->cond, &w->lock);
555 if (ret) {
556 ALOGE("Failed to wait on vsync cond %d", ret);
557 break;
558 }
559 }
560
561 if (w->exit)
562 break;
563
564 ret = pthread_mutex_unlock(&w->lock);
565 if (ret) {
566 ALOGE("Failed to unlock vsync lock %d", ret);
567 return NULL;
568 }
569
570 if (!hd->enable_vsync_events)
571 continue;
572
573 ret = hwc_wait_for_vsync(hd);
574 if (ret)
575 ALOGE("Failed to wait for vsync %d", ret);
576
577 } while (true);
578
579out:
580 ret = pthread_mutex_unlock(&hd->set_worker.lock);
581 if (ret)
582 ALOGE("Failed to unlock set lock while exiting %d", ret);
583
584 return NULL;
585}
586
587static int hwc_event_control(struct hwc_composer_device_1* dev, int display,
588 int event, int enabled)
589{
590 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
591 struct hwc_drm_display *hd = NULL;
592 int ret;
593
594 ret = hwc_get_drm_display(ctx, display, &hd);
595 if (ret)
596 return ret;
597
598 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
599 return -EINVAL;
600
601 if (hd->active_pipe == -1) {
602 ALOGD("Can't service events for display %d, no pipe", display);
603 return -EINVAL;
604 }
605
606 ret = pthread_mutex_lock(&hd->vsync_worker.lock);
607 if (ret) {
608 ALOGE("Failed to lock vsync lock %d", ret);
609 return ret;
610 }
611
612 hd->enable_vsync_events = !!enabled;
613
614 ret = pthread_cond_signal(&hd->vsync_worker.cond);
615 if (ret) {
616 ALOGE("Failed to signal vsync thread %d", ret);
617 goto out;
618 }
619
620 ret = pthread_mutex_unlock(&hd->vsync_worker.lock);
621 if (ret) {
622 ALOGE("Failed to unlock vsync lock %d", ret);
623 return ret;
624 }
625
Sean Paule0c4c3d2015-01-20 16:56:04 -0500626 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500627
628out:
629 if (pthread_mutex_unlock(&hd->vsync_worker.lock))
630 ALOGE("Failed to unlock vsync worker in error path");
631
632 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500633}
634
635static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display,
636 int mode)
637{
638 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
639 struct hwc_drm_display *hd = NULL;
640 drmModeConnectorPtr c;
641 int ret, i;
642 uint32_t dpms_prop = 0;
643 uint64_t dpms_value = 0;
644
645 ret = hwc_get_drm_display(ctx, display, &hd);
646 if (ret)
647 return ret;
648
649 c = drmModeGetConnector(ctx->fd, hd->connector_id);
650 if (!c) {
651 ALOGE("Failed to get connector %d", display);
652 return -ENODEV;
653 }
654
655 for (i = 0; !dpms_prop && i < c->count_props; i++) {
656 drmModePropertyPtr p;
657
658 p = drmModeGetProperty(ctx->fd, c->props[i]);
659 if (!p)
660 continue;
661
662 if (!strcmp(p->name, "DPMS"))
663 dpms_prop = c->props[i];
664
665 drmModeFreeProperty(p);
666 }
667 if (!dpms_prop) {
668 ALOGE("Failed to get DPMS property from display %d", display);
669 ret = -ENOENT;
670 goto out;
671 }
672
673 switch(mode) {
674 case HWC_POWER_MODE_OFF:
675 dpms_value = DRM_MODE_DPMS_OFF;
676 break;
677
678 /* We can't support dozing right now, so go full on */
679 case HWC_POWER_MODE_DOZE:
680 case HWC_POWER_MODE_DOZE_SUSPEND:
681 case HWC_POWER_MODE_NORMAL:
682 dpms_value = DRM_MODE_DPMS_ON;
683 break;
684 };
685
686 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id,
687 dpms_prop, dpms_value);
688 if (ret) {
689 ALOGE("Failed to set DPMS property for display %d", display);
690 goto out;
691 }
692
693out:
694 drmModeFreeConnector(c);
695 return ret;
696}
697
698static int hwc_query(struct hwc_composer_device_1 */* dev */, int what,
699 int *value)
700{
701 switch(what) {
702 case HWC_BACKGROUND_LAYER_SUPPORTED:
703 *value = 0; /* TODO: We should do this */
704 break;
705 case HWC_VSYNC_PERIOD:
706 ALOGW("Query for deprecated vsync value, returning 60Hz");
707 *value = 1000 * 1000 * 1000 / 60;
708 break;
709 case HWC_DISPLAY_TYPES_SUPPORTED:
710 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
711 break;
712 }
713 return 0;
714}
715
716static void hwc_register_procs(struct hwc_composer_device_1* dev,
717 hwc_procs_t const* procs)
718{
719 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
720
721 ctx->procs = procs;
722}
723
724static int hwc_get_display_configs(struct hwc_composer_device_1* dev,
725 int display, uint32_t* configs, size_t* numConfigs)
726{
727 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
728 struct hwc_drm_display *hd = NULL;
729 drmModeConnectorPtr c;
730 int ret = 0, i;
731
732 if (!*numConfigs)
733 return 0;
734
735 ret = hwc_get_drm_display(ctx, display, &hd);
736 if (ret)
737 return ret;
738
739 c = drmModeGetConnector(ctx->fd, hd->connector_id);
740 if (!c) {
741 ALOGE("Failed to get connector %d", display);
742 return -ENODEV;
743 }
744
Sean Paula4283c52015-02-04 10:08:00 -0800745 if (hd->configs) {
Sean Paule0c4c3d2015-01-20 16:56:04 -0500746 free(hd->configs);
Sean Paula4283c52015-02-04 10:08:00 -0800747 hd->configs = NULL;
748 }
749
750 if (c->connection == DRM_MODE_DISCONNECTED) {
751 ret = -ENODEV;
752 goto out;
753 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500754
Sean Paule0c4c3d2015-01-20 16:56:04 -0500755 hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes,
756 sizeof(*hd->configs));
757 if (!hd->configs) {
758 ALOGE("Failed to allocate config list for display %d", display);
759 ret = -ENOMEM;
760 hd->num_configs = 0;
761 goto out;
762 }
763
764 for (i = 0; i < c->count_modes; i++) {
765 drmModeModeInfoPtr m = &hd->configs[i];
766
767 memcpy(m, &c->modes[i], sizeof(*m));
768
769 if (i < (int)*numConfigs)
770 configs[i] = i;
771 }
772
773 hd->num_configs = c->count_modes;
774 *numConfigs = MIN(c->count_modes, *numConfigs);
775
776out:
777 drmModeFreeConnector(c);
778 return ret;
779}
780
781static int hwc_check_config_valid(struct hwc_context_t *ctx,
782 drmModeConnectorPtr connector, int display,
783 int config_idx)
784{
785 struct hwc_drm_display *hd = NULL;
786 drmModeModeInfoPtr m = NULL;
787 int ret = 0, i;
788
789 ret = hwc_get_drm_display(ctx, display, &hd);
790 if (ret)
791 return ret;
792
793 /* Make sure the requested config is still valid for the display */
794 for (i = 0; i < connector->count_modes; i++) {
795 if (hwc_mode_is_equal(&connector->modes[i],
796 &hd->configs[config_idx])) {
797 m = &hd->configs[config_idx];
798 break;
799 }
800 }
801 if (!m)
802 return -ENOENT;
803
804 return 0;
805}
806
807static int hwc_get_display_attributes(struct hwc_composer_device_1* dev,
808 int display, uint32_t config, const uint32_t* attributes,
809 int32_t* values)
810{
811 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
812 struct hwc_drm_display *hd = NULL;
813 drmModeConnectorPtr c;
814 drmModeModeInfoPtr m;
815 int ret, i;
816
817 ret = hwc_get_drm_display(ctx, display, &hd);
818 if (ret)
819 return ret;
820
821 if (config >= hd->num_configs) {
822 ALOGE("Requested config is out-of-bounds %d %d", config,
823 hd->num_configs);
824 return -EINVAL;
825 }
826
827 c = drmModeGetConnector(ctx->fd, hd->connector_id);
828 if (!c) {
829 ALOGE("Failed to get connector %d", display);
830 return -ENODEV;
831 }
832
833 ret = hwc_check_config_valid(ctx, c, display, (int)config);
834 if (ret) {
835 ALOGE("Provided config is no longer valid %u", config);
836 goto out;
837 }
838
839 m = &hd->configs[config];
840 for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
841 switch(attributes[i]) {
842 case HWC_DISPLAY_VSYNC_PERIOD:
843 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
844 break;
845 case HWC_DISPLAY_WIDTH:
846 values[i] = m->hdisplay;
847 break;
848 case HWC_DISPLAY_HEIGHT:
849 values[i] = m->vdisplay;
850 break;
851 case HWC_DISPLAY_DPI_X:
852 /* Dots per 1000 inches */
853 values[i] = c->mmWidth ?
854 (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
855 break;
856 case HWC_DISPLAY_DPI_Y:
857 /* Dots per 1000 inches */
858 values[i] = c->mmHeight ?
859 (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
860 break;
861 }
862 }
863
864out:
865 drmModeFreeConnector(c);
866 return ret;
867}
868
869static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display)
870{
871 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
872 struct hwc_drm_display *hd = NULL;
Sean Paulfa406a12015-02-04 10:05:44 -0800873 int ret, i, index = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500874
875 ret = hwc_get_drm_display(ctx, display, &hd);
876 if (ret)
877 return ret;
878
Sean Paulfa406a12015-02-04 10:05:44 -0800879 /* Find the current mode in the config list */
880 for (i = 0; i < (int)hd->num_configs; i++) {
881 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
882 index = i;
883 break;
884 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500885 }
886
Sean Paulfa406a12015-02-04 10:05:44 -0800887 return index;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500888}
889
890static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id)
891{
892 int i;
893
894 for (i = 0; i < MAX_NUM_DISPLAYS; i++) {
895 if (ctx->displays[i].active_crtc == crtc_id)
896 return true;
897 }
898 return false;
899}
900
901static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
902 uint32_t encoder_id, uint32_t *crtc_id)
903{
904 drmModeEncoderPtr e;
905 int ret, i;
906
907 e = drmModeGetEncoder(ctx->fd, encoder_id);
908 if (!e) {
909 ALOGE("Failed to get encoder for connector %d", encoder_id);
910 return -ENODEV;
911 }
912
913 /* First try to use the currently-bound crtc */
914 if (e->crtc_id) {
915 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
916 *crtc_id = e->crtc_id;
917 ret = 0;
918 goto out;
919 }
920 }
921
922 /* Try to find a possible crtc which will work */
923 for (i = 0; i < r->count_crtcs; i++) {
924 if (!(e->possible_crtcs & (1 << i)))
925 continue;
926
927 /* We've already tried this earlier */
928 if (e->crtc_id == r->crtcs[i])
929 continue;
930
931 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
932 *crtc_id = r->crtcs[i];
933 ret = 0;
934 goto out;
935 }
936 }
937
938 /* We can't use the encoder, but nothing went wrong, try another one */
939 ret = -EAGAIN;
940
941out:
942 drmModeFreeEncoder(e);
943 return ret;
944}
945
946static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display,
947 int index)
948{
949 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
950 struct hwc_drm_display *hd = NULL;
951 drmModeResPtr r = NULL;
952 drmModeConnectorPtr c;
953 uint32_t crtc_id = 0;
954 int ret, i;
955 bool new_crtc, new_encoder;
956
957 ret = hwc_get_drm_display(ctx, display, &hd);
958 if (ret)
959 return ret;
960
961 c = drmModeGetConnector(ctx->fd, hd->connector_id);
962 if (!c) {
963 ALOGE("Failed to get connector %d", display);
964 return -ENODEV;
965 }
966
967 if (c->connection == DRM_MODE_DISCONNECTED) {
968 ALOGE("Tried to configure a disconnected display %d", display);
969 ret = -ENODEV;
970 goto out;
971 }
972
Sean Paulfa406a12015-02-04 10:05:44 -0800973 if (index >= c->count_modes) {
974 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500975 ret = -ENOENT;
976 goto out;
977 }
978
979 r = drmModeGetResources(ctx->fd);
980 if (!r) {
981 ALOGE("Failed to get drm resources");
982 goto out;
983 }
984
985 /* We no longer have an active_crtc */
986 hd->active_crtc = 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500987 hd->active_pipe = -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500988
989 /* First, try to use the currently-connected encoder */
990 if (c->encoder_id) {
991 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
992 if (ret && ret != -EAGAIN) {
993 ALOGE("Encoder try failed %d", ret);
994 goto out;
995 }
996 }
997
998 /* We couldn't find a crtc with the attached encoder, try the others */
999 if (!crtc_id) {
1000 for (i = 0; i < c->count_encoders; i++) {
1001 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
1002 if (!ret) {
1003 break;
1004 } else if (ret != -EAGAIN) {
1005 ALOGE("Encoder try failed %d", ret);
1006 goto out;
1007 }
1008 }
1009 if (!crtc_id) {
1010 ALOGE("Couldn't find valid crtc to modeset");
1011 ret = -EINVAL;
1012 goto out;
1013 }
1014 }
1015
1016 hd->active_crtc = crtc_id;
Sean Paulfa406a12015-02-04 10:05:44 -08001017
1018 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
Sean Paule0c4c3d2015-01-20 16:56:04 -05001019
Sean Pauleb9e75c2015-01-25 23:31:30 -05001020 /* Find the pipe corresponding to the crtc_id */
1021 for (i = 0; i < r->count_crtcs; i++) {
1022 /* We've already tried this earlier */
1023 if (r->crtcs[i] == crtc_id) {
1024 hd->active_pipe = i;
1025 break;
1026 }
1027 }
1028 /* This should never happen... hehehe */
1029 if (hd->active_pipe == -1) {
1030 ALOGE("Active crtc was not found in resources!!");
1031 ret = -ENODEV;
1032 goto out;
1033 }
1034
Sean Paule0c4c3d2015-01-20 16:56:04 -05001035 /* TODO: Once we have atomic, set the crtc timing info here */
1036
1037out:
1038 if (r)
1039 drmModeFreeResources(r);
1040
1041 drmModeFreeConnector(c);
1042 return ret;
1043}
1044
Sean Paul9aa5ad32015-01-22 15:47:54 -05001045static int hwc_destroy_worker(struct hwc_worker *worker)
1046{
1047 int ret;
1048
1049 ret = pthread_mutex_lock(&worker->lock);
1050 if (ret) {
1051 ALOGE("Failed to lock in destroy() %d", ret);
1052 return ret;
1053 }
1054
1055 worker->exit = true;
1056
1057 ret |= pthread_cond_signal(&worker->cond);
1058 if (ret)
1059 ALOGE("Failed to signal cond in destroy() %d", ret);
1060
1061 ret |= pthread_mutex_unlock(&worker->lock);
1062 if (ret)
1063 ALOGE("Failed to unlock in destroy() %d", ret);
1064
1065 ret |= pthread_join(worker->thread, NULL);
1066 if (ret && ret != ESRCH)
1067 ALOGE("Failed to join thread in destroy() %d", ret);
1068
1069 return ret;
1070}
1071
1072static void hwc_destroy_display(struct hwc_drm_display *hd)
1073{
1074 int ret;
1075
1076 if (hwc_destroy_worker(&hd->set_worker))
1077 ALOGE("Destroy set worker failed");
Sean Pauleb9e75c2015-01-25 23:31:30 -05001078
1079 if (hwc_destroy_worker(&hd->vsync_worker))
1080 ALOGE("Destroy vsync worker failed");
Sean Paul9aa5ad32015-01-22 15:47:54 -05001081}
1082
Sean Paule0c4c3d2015-01-20 16:56:04 -05001083static int hwc_device_close(struct hw_device_t *dev)
1084{
1085 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulcd36a9e2015-01-22 18:01:18 -05001086 int ret, i;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001087
Sean Paul9aa5ad32015-01-22 15:47:54 -05001088 for (i = 0; i < MAX_NUM_DISPLAYS; i++)
1089 hwc_destroy_display(&ctx->displays[i]);
1090
1091 drmClose(ctx->fd);
Sean Paulcd36a9e2015-01-22 18:01:18 -05001092
1093 ret = hwc_import_destroy(ctx->import_ctx);
1094 if (ret)
1095 ALOGE("Could not destroy import %d", ret);
1096
Sean Paule0c4c3d2015-01-20 16:56:04 -05001097 free(ctx);
1098
1099 return 0;
1100}
1101
Sean Paul9aa5ad32015-01-22 15:47:54 -05001102static int hwc_initialize_worker(struct hwc_drm_display *hd,
1103 struct hwc_worker *worker, void *(*routine)(void*))
1104{
1105 int ret;
1106
1107 ret = pthread_cond_init(&worker->cond, NULL);
1108 if (ret) {
1109 ALOGE("Failed to create worker condition %d", ret);
1110 return ret;
1111 }
1112
1113 ret = pthread_mutex_init(&worker->lock, NULL);
1114 if (ret) {
1115 ALOGE("Failed to initialize worker lock %d", ret);
1116 goto err_cond;
1117 }
1118
1119 worker->exit = false;
1120
1121 ret = pthread_create(&worker->thread, NULL, routine, hd);
1122 if (ret) {
1123 ALOGE("Could not create worker thread %d", ret);
1124 goto err_lock;
1125 }
1126 return 0;
1127
1128err_lock:
1129 pthread_mutex_destroy(&worker->lock);
1130err_cond:
1131 pthread_cond_destroy(&worker->cond);
1132 return ret;
1133}
1134
Sean Paul24a26e32015-02-04 10:34:47 -08001135/*
1136 * TODO: This function sets the active config to the first one in the list. This
1137 * should be fixed such that it selects the preferred mode for the display, or
1138 * some other, saner, method of choosing the config.
1139 */
1140static int hwc_set_initial_config(struct hwc_drm_display *hd)
1141{
1142 int ret;
1143 uint32_t config;
1144 size_t num_configs = 1;
1145
1146 ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1147 &num_configs);
Sean Paulaea15c22015-02-09 02:24:11 -05001148 if (ret || !num_configs)
Sean Paul24a26e32015-02-04 10:34:47 -08001149 return 0;
1150
1151 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1152 if (ret) {
1153 ALOGE("Failed to set active config d=%d ret=%d", hd->display,
1154 ret);
1155 return ret;
1156 }
1157
1158 return ret;
1159}
1160
Sean Paule0c4c3d2015-01-20 16:56:04 -05001161static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1162 uint32_t connector_id)
1163{
1164 struct hwc_drm_display *hd = NULL;
1165 int ret;
1166
1167 ret = hwc_get_drm_display(ctx, display, &hd);
1168 if (ret)
1169 return ret;
1170
Sean Paul9aa5ad32015-01-22 15:47:54 -05001171 hd->ctx = ctx;
1172 hd->display = display;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001173 hd->active_pipe = -1;
Sean Paulefb20cb2015-02-04 09:29:15 -08001174 hd->initial_modeset_required = true;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001175 hd->connector_id = connector_id;
1176
Sean Paulf1dc1912015-01-24 01:34:31 -05001177 ret = sw_sync_timeline_create();
1178 if (ret < 0) {
1179 ALOGE("Failed to create sw sync timeline %d", ret);
1180 return ret;
1181 }
1182 hd->timeline_fd = ret;
Sean Paule147a2a2015-02-22 17:55:43 -05001183
1184 /*
1185 * Initialize timeline_next to 1, because point 0 will be the very first
1186 * set operation. Since we increment every time set() is called,
1187 * initializing to 0 would cause an off-by-one error where
1188 * surfaceflinger would composite on the front buffer.
1189 */
1190 hd->timeline_next = 1;
Sean Paulf1dc1912015-01-24 01:34:31 -05001191
Sean Paul24a26e32015-02-04 10:34:47 -08001192 ret = hwc_set_initial_config(hd);
1193 if (ret) {
1194 ALOGE("Failed to set initial config for d=%d ret=%d", display,
1195 ret);
1196 return ret;
1197 }
1198
Sean Paul9aa5ad32015-01-22 15:47:54 -05001199 ret = hwc_initialize_worker(hd, &hd->set_worker, hwc_set_worker);
1200 if (ret) {
1201 ALOGE("Failed to create set worker %d\n", ret);
1202 return ret;
1203 }
1204
Sean Pauleb9e75c2015-01-25 23:31:30 -05001205 ret = hwc_initialize_worker(hd, &hd->vsync_worker, hwc_vsync_worker);
1206 if (ret) {
1207 ALOGE("Failed to create vsync worker %d", ret);
1208 goto err;
1209 }
1210
Sean Paule0c4c3d2015-01-20 16:56:04 -05001211 return 0;
Sean Pauleb9e75c2015-01-25 23:31:30 -05001212
1213err:
1214 if (hwc_destroy_worker(&hd->set_worker))
1215 ALOGE("Failed to destroy set worker");
1216
1217 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -05001218}
1219
1220static int hwc_enumerate_displays(struct hwc_context_t *ctx)
1221{
1222 struct hwc_drm_display *panel_hd;
1223 drmModeResPtr res;
1224 drmModeConnectorPtr *conn_list;
1225 int ret = 0, i, j;
1226
1227 res = drmModeGetResources(ctx->fd);
1228 if (!res) {
1229 ALOGE("Failed to get drm resources");
1230 return -ENODEV;
1231 }
1232
1233 conn_list = (drmModeConnector **)calloc(res->count_connectors,
1234 sizeof(*conn_list));
1235 if (!conn_list) {
1236 ALOGE("Failed to allocate connector list");
1237 ret = -ENOMEM;
1238 goto out;
1239 }
1240
1241 for (i = 0; i < res->count_connectors; i++) {
1242 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1243 if (!conn_list[i]) {
1244 ALOGE("Failed to get connector %d", res->connectors[i]);
1245 ret = -ENODEV;
1246 goto out;
1247 }
1248 }
1249
1250 ctx->num_displays = 0;
1251
1252 /* Find a connected, panel type connector for display 0 */
1253 for (i = 0; i < res->count_connectors; i++) {
1254 drmModeConnectorPtr c = conn_list[i];
1255
1256 for (j = 0; j < ARRAY_SIZE(panel_types); j++) {
1257 if (c->connector_type == panel_types[j] &&
1258 c->connection == DRM_MODE_CONNECTED)
1259 break;
1260 }
1261 if (j == ARRAY_SIZE(panel_types))
1262 continue;
1263
1264 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1265 ctx->num_displays++;
1266 break;
1267 }
1268
1269 ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1270 if (ret)
1271 goto out;
1272
1273 /* Fill in the other displays */
1274 for (i = 0; i < res->count_connectors; i++) {
1275 drmModeConnectorPtr c = conn_list[i];
1276
1277 if (panel_hd->connector_id == c->connector_id)
1278 continue;
1279
1280 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1281 ctx->num_displays++;
1282 }
1283
1284out:
1285 for (i = 0; i < res->count_connectors; i++) {
1286 if (conn_list[i])
1287 drmModeFreeConnector(conn_list[i]);
1288 }
1289 free(conn_list);
1290
1291 if (res)
1292 drmModeFreeResources(res);
1293
1294 return ret;
1295}
1296
1297static int hwc_device_open(const struct hw_module_t* module, const char* name,
1298 struct hw_device_t** dev)
1299{
1300 int ret = 0;
1301 struct hwc_context_t *ctx;
Lauri Peltonen64717b22015-02-04 16:55:31 +02001302 char path[PROPERTY_VALUE_MAX];
Sean Paule0c4c3d2015-01-20 16:56:04 -05001303
1304 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1305 ALOGE("Invalid module name- %s", name);
1306 return -EINVAL;
1307 }
1308
Sean Paul9b1bb842015-01-23 01:11:58 -05001309 ctx = new hwc_context_t();
Sean Paule0c4c3d2015-01-20 16:56:04 -05001310 if (!ctx) {
1311 ALOGE("Failed to allocate hwc context");
1312 return -ENOMEM;
1313 }
1314
Sean Paulcd36a9e2015-01-22 18:01:18 -05001315 ret = hwc_import_init(&ctx->import_ctx);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001316 if (ret) {
Sean Paulcd36a9e2015-01-22 18:01:18 -05001317 ALOGE("Failed to initialize import context");
Sean Paule0c4c3d2015-01-20 16:56:04 -05001318 goto out;
1319 }
1320
Lauri Peltonen64717b22015-02-04 16:55:31 +02001321 property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001322 /* TODO: Use drmOpenControl here instead */
Lauri Peltonen64717b22015-02-04 16:55:31 +02001323 ctx->fd = open(path, O_RDWR);
Sean Paule0c4c3d2015-01-20 16:56:04 -05001324 if (ctx->fd < 0) {
1325 ALOGE("Failed to open dri- %s", strerror(-errno));
1326 goto out;
1327 }
1328
Sean Paule0c4c3d2015-01-20 16:56:04 -05001329 ret = hwc_enumerate_displays(ctx);
1330 if (ret) {
1331 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1332 goto out;
1333 }
1334
1335 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1336 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1337 ctx->device.common.module = const_cast<hw_module_t*>(module);
1338 ctx->device.common.close = hwc_device_close;
1339
1340 ctx->device.prepare = hwc_prepare;
1341 ctx->device.set = hwc_set;
1342 ctx->device.eventControl = hwc_event_control;
1343 ctx->device.setPowerMode = hwc_set_power_mode;
1344 ctx->device.query = hwc_query;
1345 ctx->device.registerProcs = hwc_register_procs;
1346 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1347 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1348 ctx->device.getActiveConfig = hwc_get_active_config;
1349 ctx->device.setActiveConfig = hwc_set_active_config;
1350 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1351
1352 *dev = &ctx->device.common;
1353
1354 return 0;
1355out:
1356 if (ctx->fd >= 0)
1357 close(ctx->fd);
1358
1359 free(ctx);
1360 return ret;
1361}
1362
1363static struct hw_module_methods_t hwc_module_methods = {
1364 open: hwc_device_open
1365};
1366
1367hwc_module_t HAL_MODULE_INFO_SYM = {
1368 common: {
1369 tag: HARDWARE_MODULE_TAG,
1370 version_major: 1,
1371 version_minor: 0,
1372 id: HWC_HARDWARE_MODULE_ID,
1373 name: "DRM hwcomposer module",
1374 author: "The Android Open Source Project",
1375 methods: &hwc_module_methods,
1376 dso: NULL,
1377 reserved: { 0 },
1378 }
1379};