2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwcomposer-drm"
22 #include <sys/param.h>
23 #include <sys/resource.h>
26 #include <cutils/log.h>
29 #include <xf86drmMode.h>
31 #include <hardware/hardware.h>
32 #include <hardware/hwcomposer.h>
34 #include <cutils/properties.h>
35 #include <sync/sync.h>
38 #include "drm_hwcomposer.h"
40 #define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0]))
42 #define HWCOMPOSER_DRM_DEVICE "/dev/dri/card0"
43 #define MAX_NUM_DISPLAYS 3
44 #define UM_PER_INCH 25400
46 static const uint32_t panel_types[] = {
47 DRM_MODE_CONNECTOR_LVDS,
48 DRM_MODE_CONNECTOR_eDP,
49 DRM_MODE_CONNECTOR_DSI,
59 struct hwc_drm_display {
60 struct hwc_context_t *ctx;
63 uint32_t connector_id;
65 drmModeModeInfoPtr configs;
68 drmModeModeInfo active_mode;
71 bool initial_modeset_required;
73 struct hwc_worker set_worker;
75 std::list<struct hwc_drm_bo> buf_queue;
76 struct hwc_drm_bo front;
77 pthread_mutex_t flip_lock;
78 pthread_cond_t flip_cond;
81 unsigned timeline_next;
83 bool enable_vsync_events;
84 unsigned int vsync_sequence;
87 struct hwc_context_t {
88 hwc_composer_device_1_t device;
92 hwc_procs_t const *procs;
93 struct hwc_import_context *import_ctx;
95 struct hwc_drm_display displays[MAX_NUM_DISPLAYS];
98 struct hwc_worker event_worker;
101 static int hwc_get_drm_display(struct hwc_context_t *ctx, int display,
102 struct hwc_drm_display **hd)
104 if (display >= MAX_NUM_DISPLAYS) {
105 ALOGE("Requested display is out-of-bounds %d %d", display,
109 *hd = &ctx->displays[display];
113 static int hwc_prepare_layer(hwc_layer_1_t *layer)
115 /* TODO: We can't handle background right now, defer to sufaceFlinger */
116 if (layer->compositionType == HWC_BACKGROUND) {
117 layer->compositionType = HWC_FRAMEBUFFER;
118 ALOGV("Can't handle background layers yet");
120 /* TODO: Support sideband compositions */
121 } else if (layer->compositionType == HWC_SIDEBAND) {
122 layer->compositionType = HWC_FRAMEBUFFER;
123 ALOGV("Can't handle sideband content yet");
128 /* TODO: Handle cursor by setting compositionType=HWC_CURSOR_OVERLAY */
129 if (layer->flags & HWC_IS_CURSOR_LAYER) {
130 ALOGV("Can't handle async cursors yet");
133 /* TODO: Handle transformations */
134 if (layer->transform) {
135 ALOGV("Can't handle transformations yet");
138 /* TODO: Handle blending & plane alpha*/
139 if (layer->blending == HWC_BLENDING_PREMULT ||
140 layer->blending == HWC_BLENDING_COVERAGE) {
141 ALOGV("Can't handle blending yet");
144 /* TODO: Handle cropping & scaling */
149 static int hwc_prepare(hwc_composer_device_1_t */* dev */, size_t num_displays,
150 hwc_display_contents_1_t** display_contents)
154 /* TODO: Check flags for HWC_GEOMETRY_CHANGED */
156 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
158 if (!display_contents[i])
161 for (j = 0; j < (int)display_contents[i]->numHwLayers; j++) {
162 ret = hwc_prepare_layer(
163 &display_contents[i]->hwLayers[j]);
165 ALOGE("Failed to prepare layer %d:%d", j, i);
174 static int hwc_queue_vblank_event(struct hwc_drm_display *hd)
181 if (hd->active_pipe == -1) {
182 ALOGE("Active pipe is -1 disp=%d", hd->display);
186 memset(&vblank, 0, sizeof(vblank));
188 high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
189 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_ABSOLUTE |
190 DRM_VBLANK_NEXTONMISS | DRM_VBLANK_EVENT |
191 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
192 vblank.request.signal = (unsigned long)hd;
193 vblank.request.sequence = hd->vsync_sequence + 1;
195 ret = drmWaitVBlank(hd->ctx->fd, &vblank);
197 ALOGE("Failed to wait for vblank %d", ret);
204 static void hwc_vblank_event_handler(int /* fd */, unsigned int sequence,
205 unsigned int tv_sec, unsigned int tv_usec,
208 struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data;
212 if (!hd->enable_vsync_events || !hd->ctx->procs->vsync)
216 * Discard duplicate vsync (can happen when enabling vsync events while
217 * already processing vsyncs).
219 if (sequence <= hd->vsync_sequence)
222 hd->vsync_sequence = sequence;
223 ret = hwc_queue_vblank_event(hd);
225 ALOGE("Failed to queue vblank event ret=%d", ret);
227 timestamp = (int64_t)tv_sec * 1000 * 1000 * 1000 +
228 (int64_t)tv_usec * 1000;
229 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
232 static void hwc_flip_event_handler(int /* fd */, unsigned int /* sequence */,
233 unsigned int /* tv_sec */, unsigned int /* tv_usec */,
236 struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data;
240 ret = pthread_mutex_lock(&hd->flip_lock);
242 ALOGE("Failed to lock flip lock ret=%d", ret);
246 ret = pthread_cond_signal(&hd->flip_cond);
248 ALOGE("Failed to signal flip condition ret=%d", ret);
253 ret = pthread_mutex_unlock(&hd->flip_lock);
255 ALOGE("Failed to unlock flip lock ret=%d", ret);
260 static void *hwc_event_worker(void *arg)
262 struct hwc_context_t *ctx = (struct hwc_context_t *)arg;
265 drmEventContext event_context;
267 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
271 FD_SET(ctx->fd, &fds);
273 event_context.version = DRM_EVENT_CONTEXT_VERSION;
274 event_context.page_flip_handler = hwc_flip_event_handler;
275 event_context.vblank_handler = hwc_vblank_event_handler;
278 ret = select(ctx->fd + 1, &fds, NULL, NULL, NULL);
279 } while (ret == -1 && errno == EINTR);
282 ALOGE("Failed waiting for drm event\n");
286 drmHandleEvent(ctx->fd, &event_context);
292 static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b)
294 return a->clock == b->clock &&
295 a->hdisplay == b->hdisplay &&
296 a->hsync_start == b->hsync_start &&
297 a->hsync_end == b->hsync_end &&
298 a->htotal == b->htotal &&
299 a->hskew == b->hskew &&
300 a->vdisplay == b->vdisplay &&
301 a->vsync_start == b->vsync_start &&
302 a->vsync_end == b->vsync_end &&
303 a->vtotal == b->vtotal &&
304 a->vscan == b->vscan &&
305 a->vrefresh == b->vrefresh &&
306 a->flags == b->flags &&
307 a->type == b->type &&
308 !strcmp(a->name, b->name);
311 static int hwc_modeset_required(struct hwc_drm_display *hd,
312 bool *modeset_required)
315 drmModeModeInfoPtr m;
317 if (hd->initial_modeset_required) {
318 *modeset_required = true;
319 hd->initial_modeset_required = false;
323 crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc);
325 ALOGE("Failed to get crtc for display %d", hd->display);
329 m = &hd->active_mode;
331 /* Do a modeset if we haven't done one, or the mode has changed */
332 if (!crtc->mode_valid || !hwc_mode_is_equal(m, &crtc->mode))
333 *modeset_required = true;
335 *modeset_required = false;
337 drmModeFreeCrtc(crtc);
342 static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf)
345 bool modeset_required;
347 ret = hwc_modeset_required(hd, &modeset_required);
349 ALOGE("Failed to determine if modeset is required %d", ret);
352 if (modeset_required) {
353 ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id,
354 0, 0, &hd->connector_id, 1,
357 ALOGE("Modeset failed for crtc %d",
364 ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id,
365 DRM_MODE_PAGE_FLIP_EVENT, hd);
367 ALOGE("Failed to flip buffer for crtc %d",
372 ret = pthread_cond_wait(&hd->flip_cond, &hd->flip_lock);
374 ALOGE("Failed to wait on condition %d", ret);
381 static int hwc_wait_and_set(struct hwc_drm_display *hd,
382 struct hwc_drm_bo *buf)
384 struct drm_gem_close args;
387 if (buf->acquire_fence_fd >= 0) {
388 ret = sync_wait(buf->acquire_fence_fd, -1);
389 close(buf->acquire_fence_fd);
390 buf->acquire_fence_fd = -1;
392 ALOGE("Failed to wait for acquire %d", ret);
397 ret = hwc_flip(hd, buf);
399 ALOGE("Failed to perform flip\n");
403 if (hwc_import_bo_release(hd->ctx->fd, hd->ctx->import_ctx, &hd->front)) {
404 memset(&args, 0, sizeof(args));
405 for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
406 if (!hd->front.gem_handles[i])
409 /* check for duplicate handle in buf_queue */
412 ret = pthread_mutex_lock(&hd->set_worker.lock);
414 ALOGE("Failed to lock set lock in wait_and_set() %d", ret);
419 for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin();
420 bi != hd->buf_queue.end();
422 for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++)
423 if (hd->front.gem_handles[i] == bi->gem_handles[j] )
426 for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); j++)
427 if (hd->front.gem_handles[i] == buf->gem_handles[j])
431 args.handle = hd->front.gem_handles[i];
432 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
434 if (pthread_mutex_unlock(&hd->set_worker.lock))
435 ALOGE("Failed to unlock set lock in wait_and_set() %d", ret);
444 static void *hwc_set_worker(void *arg)
446 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
449 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
451 ret = pthread_mutex_lock(&hd->flip_lock);
453 ALOGE("Failed to lock flip lock ret=%d", ret);
458 struct hwc_drm_bo buf;
460 ret = pthread_mutex_lock(&hd->set_worker.lock);
462 ALOGE("Failed to lock set lock %d", ret);
466 if (hd->set_worker.exit)
469 if (hd->buf_queue.empty()) {
470 ret = pthread_cond_wait(&hd->set_worker.cond,
471 &hd->set_worker.lock);
473 ALOGE("Failed to wait on condition %d", ret);
478 buf = hd->buf_queue.front();
479 hd->buf_queue.pop_front();
481 ret = pthread_mutex_unlock(&hd->set_worker.lock);
483 ALOGE("Failed to unlock set lock %d", ret);
487 ret = hwc_wait_and_set(hd, &buf);
489 ALOGE("Failed to wait and set %d", ret);
491 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
493 ALOGE("Failed to increment sync timeline %d", ret);
497 ret = pthread_mutex_unlock(&hd->set_worker.lock);
499 ALOGE("Failed to unlock set lock while exiting %d", ret);
501 ret = pthread_mutex_unlock(&hd->flip_lock);
503 ALOGE("Failed to unlock flip lock ret=%d", ret);
509 static int hwc_set_display(hwc_context_t *ctx, int display,
510 hwc_display_contents_1_t* display_contents)
512 struct hwc_drm_display *hd = NULL;
513 hwc_layer_1_t *layer = NULL;
514 struct hwc_drm_bo buf;
518 memset(&buf, 0, sizeof(buf));
520 ret = hwc_get_drm_display(ctx, display, &hd);
524 if (!hd->active_crtc) {
525 ALOGE("There is no active crtc for display %d", display);
531 * TODO: We can only support one hw layer atm, so choose either the
532 * first one or the framebuffer target.
534 if (!display_contents->numHwLayers) {
536 } else if (display_contents->numHwLayers == 1) {
537 layer = &display_contents->hwLayers[0];
539 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
540 layer = &display_contents->hwLayers[i];
541 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
544 if (i == (int)display_contents->numHwLayers) {
545 ALOGE("Could not find a suitable layer for display %d",
551 ret = pthread_mutex_lock(&hd->set_worker.lock);
553 ALOGE("Failed to lock set lock in set() %d", ret);
557 ret = hwc_import_bo_create(ctx->fd, ctx->import_ctx, layer->handle,
560 ALOGE("Failed to import handle to drm bo %d", ret);
563 buf.acquire_fence_fd = layer->acquireFenceFd;
564 layer->acquireFenceFd = -1;
567 * TODO: Retire and release can use the same sync point here b/c hwc is
568 * restricted to one layer. Once that is no longer true, this will need
572 display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd,
573 "drm_hwc_retire", hd->timeline_next);
574 layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd,
575 "drm_hwc_release", hd->timeline_next);
576 hd->buf_queue.push_back(buf);
578 ret = pthread_cond_signal(&hd->set_worker.cond);
580 ALOGE("Failed to signal set worker %d", ret);
582 if (pthread_mutex_unlock(&hd->set_worker.lock))
583 ALOGE("Failed to unlock set lock in set()");
586 /* Close input fences. */
587 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
588 layer = &display_contents->hwLayers[i];
589 if (layer->acquireFenceFd >= 0) {
590 close(layer->acquireFenceFd);
591 layer->acquireFenceFd = -1;
594 if (display_contents->outbufAcquireFenceFd >= 0) {
595 close(display_contents->outbufAcquireFenceFd);
596 display_contents->outbufAcquireFenceFd = -1;
602 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
603 hwc_display_contents_1_t** display_contents)
605 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
608 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
609 if (display_contents[i])
610 ret = hwc_set_display(ctx, i, display_contents[i]);
616 static int hwc_event_control(struct hwc_composer_device_1* dev, int display,
617 int event, int enabled)
619 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
620 struct hwc_drm_display *hd = NULL;
623 ret = hwc_get_drm_display(ctx, display, &hd);
627 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
630 if (hd->active_pipe == -1) {
631 ALOGD("Can't service events for display %d, no pipe", display);
635 hd->enable_vsync_events = !!enabled;
637 if (!hd->enable_vsync_events)
641 * Note that it's possible that the event worker is already waiting for
642 * a vsync, and this will be a duplicate request. In that event, we'll
643 * end up firing the event handler twice, and it will discard the second
644 * event. Not ideal, but not worth introducing a bunch of additional
645 * logic/locks/state for.
647 ret = hwc_queue_vblank_event(hd);
649 ALOGE("Failed to queue vblank event ret=%d", ret);
656 static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display,
659 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
660 struct hwc_drm_display *hd = NULL;
661 drmModeConnectorPtr c;
663 uint32_t dpms_prop = 0;
664 uint64_t dpms_value = 0;
666 ret = hwc_get_drm_display(ctx, display, &hd);
670 c = drmModeGetConnector(ctx->fd, hd->connector_id);
672 ALOGE("Failed to get connector %d", display);
676 for (i = 0; !dpms_prop && i < c->count_props; i++) {
677 drmModePropertyPtr p;
679 p = drmModeGetProperty(ctx->fd, c->props[i]);
683 if (!strcmp(p->name, "DPMS"))
684 dpms_prop = c->props[i];
686 drmModeFreeProperty(p);
689 ALOGE("Failed to get DPMS property from display %d", display);
695 case HWC_POWER_MODE_OFF:
696 dpms_value = DRM_MODE_DPMS_OFF;
699 /* We can't support dozing right now, so go full on */
700 case HWC_POWER_MODE_DOZE:
701 case HWC_POWER_MODE_DOZE_SUSPEND:
702 case HWC_POWER_MODE_NORMAL:
703 dpms_value = DRM_MODE_DPMS_ON;
707 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id,
708 dpms_prop, dpms_value);
710 ALOGE("Failed to set DPMS property for display %d", display);
715 drmModeFreeConnector(c);
719 static int hwc_query(struct hwc_composer_device_1 */* dev */, int what,
723 case HWC_BACKGROUND_LAYER_SUPPORTED:
724 *value = 0; /* TODO: We should do this */
726 case HWC_VSYNC_PERIOD:
727 ALOGW("Query for deprecated vsync value, returning 60Hz");
728 *value = 1000 * 1000 * 1000 / 60;
730 case HWC_DISPLAY_TYPES_SUPPORTED:
731 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
737 static void hwc_register_procs(struct hwc_composer_device_1* dev,
738 hwc_procs_t const* procs)
740 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
745 static int hwc_get_display_configs(struct hwc_composer_device_1* dev,
746 int display, uint32_t* configs, size_t* numConfigs)
748 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
749 struct hwc_drm_display *hd = NULL;
750 drmModeConnectorPtr c;
756 ret = hwc_get_drm_display(ctx, display, &hd);
760 c = drmModeGetConnector(ctx->fd, hd->connector_id);
762 ALOGE("Failed to get connector %d", display);
771 if (c->connection == DRM_MODE_DISCONNECTED) {
776 hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes,
777 sizeof(*hd->configs));
779 ALOGE("Failed to allocate config list for display %d", display);
785 for (i = 0; i < c->count_modes; i++) {
786 drmModeModeInfoPtr m = &hd->configs[i];
788 memcpy(m, &c->modes[i], sizeof(*m));
790 if (i < (int)*numConfigs)
794 hd->num_configs = c->count_modes;
795 *numConfigs = MIN(c->count_modes, *numConfigs);
798 drmModeFreeConnector(c);
802 static int hwc_check_config_valid(struct hwc_context_t *ctx,
803 drmModeConnectorPtr connector, int display,
806 struct hwc_drm_display *hd = NULL;
807 drmModeModeInfoPtr m = NULL;
810 ret = hwc_get_drm_display(ctx, display, &hd);
814 /* Make sure the requested config is still valid for the display */
815 for (i = 0; i < connector->count_modes; i++) {
816 if (hwc_mode_is_equal(&connector->modes[i],
817 &hd->configs[config_idx])) {
818 m = &hd->configs[config_idx];
828 static int hwc_get_display_attributes(struct hwc_composer_device_1* dev,
829 int display, uint32_t config, const uint32_t* attributes,
832 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
833 struct hwc_drm_display *hd = NULL;
834 drmModeConnectorPtr c;
835 drmModeModeInfoPtr m;
838 ret = hwc_get_drm_display(ctx, display, &hd);
842 if (config >= hd->num_configs) {
843 ALOGE("Requested config is out-of-bounds %d %d", config,
848 c = drmModeGetConnector(ctx->fd, hd->connector_id);
850 ALOGE("Failed to get connector %d", display);
854 ret = hwc_check_config_valid(ctx, c, display, (int)config);
856 ALOGE("Provided config is no longer valid %u", config);
860 m = &hd->configs[config];
861 for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
862 switch(attributes[i]) {
863 case HWC_DISPLAY_VSYNC_PERIOD:
864 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
866 case HWC_DISPLAY_WIDTH:
867 values[i] = m->hdisplay;
869 case HWC_DISPLAY_HEIGHT:
870 values[i] = m->vdisplay;
872 case HWC_DISPLAY_DPI_X:
873 /* Dots per 1000 inches */
874 values[i] = c->mmWidth ?
875 (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
877 case HWC_DISPLAY_DPI_Y:
878 /* Dots per 1000 inches */
879 values[i] = c->mmHeight ?
880 (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
886 drmModeFreeConnector(c);
890 static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display)
892 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
893 struct hwc_drm_display *hd = NULL;
894 int ret, i, index = -1;
896 ret = hwc_get_drm_display(ctx, display, &hd);
900 /* Find the current mode in the config list */
901 for (i = 0; i < (int)hd->num_configs; i++) {
902 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
911 static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id)
915 for (i = 0; i < MAX_NUM_DISPLAYS; i++) {
916 if (ctx->displays[i].active_crtc == crtc_id)
922 static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
923 uint32_t encoder_id, uint32_t *crtc_id)
928 e = drmModeGetEncoder(ctx->fd, encoder_id);
930 ALOGE("Failed to get encoder for connector %d", encoder_id);
934 /* First try to use the currently-bound crtc */
936 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
937 *crtc_id = e->crtc_id;
943 /* Try to find a possible crtc which will work */
944 for (i = 0; i < r->count_crtcs; i++) {
945 if (!(e->possible_crtcs & (1 << i)))
948 /* We've already tried this earlier */
949 if (e->crtc_id == r->crtcs[i])
952 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
953 *crtc_id = r->crtcs[i];
959 /* We can't use the encoder, but nothing went wrong, try another one */
963 drmModeFreeEncoder(e);
967 static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display,
970 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
971 struct hwc_drm_display *hd = NULL;
972 drmModeResPtr r = NULL;
973 drmModeConnectorPtr c;
974 uint32_t crtc_id = 0;
976 bool new_crtc, new_encoder;
978 ret = hwc_get_drm_display(ctx, display, &hd);
982 c = drmModeGetConnector(ctx->fd, hd->connector_id);
984 ALOGE("Failed to get connector %d", display);
988 if (c->connection == DRM_MODE_DISCONNECTED) {
989 ALOGE("Tried to configure a disconnected display %d", display);
994 if (index >= c->count_modes) {
995 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
1000 r = drmModeGetResources(ctx->fd);
1002 ALOGE("Failed to get drm resources");
1006 /* We no longer have an active_crtc */
1007 hd->active_crtc = 0;
1008 hd->active_pipe = -1;
1010 /* First, try to use the currently-connected encoder */
1011 if (c->encoder_id) {
1012 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
1013 if (ret && ret != -EAGAIN) {
1014 ALOGE("Encoder try failed %d", ret);
1019 /* We couldn't find a crtc with the attached encoder, try the others */
1021 for (i = 0; i < c->count_encoders; i++) {
1022 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
1025 } else if (ret != -EAGAIN) {
1026 ALOGE("Encoder try failed %d", ret);
1031 ALOGE("Couldn't find valid crtc to modeset");
1037 hd->active_crtc = crtc_id;
1039 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
1041 /* Find the pipe corresponding to the crtc_id */
1042 for (i = 0; i < r->count_crtcs; i++) {
1043 /* We've already tried this earlier */
1044 if (r->crtcs[i] == crtc_id) {
1045 hd->active_pipe = i;
1049 /* This should never happen... hehehe */
1050 if (hd->active_pipe == -1) {
1051 ALOGE("Active crtc was not found in resources!!");
1056 /* TODO: Once we have atomic, set the crtc timing info here */
1060 drmModeFreeResources(r);
1062 drmModeFreeConnector(c);
1066 static int hwc_destroy_worker(struct hwc_worker *worker)
1070 ret = pthread_mutex_lock(&worker->lock);
1072 ALOGE("Failed to lock in destroy() %d", ret);
1076 worker->exit = true;
1078 ret |= pthread_cond_signal(&worker->cond);
1080 ALOGE("Failed to signal cond in destroy() %d", ret);
1082 ret |= pthread_mutex_unlock(&worker->lock);
1084 ALOGE("Failed to unlock in destroy() %d", ret);
1086 ret |= pthread_join(worker->thread, NULL);
1087 if (ret && ret != ESRCH)
1088 ALOGE("Failed to join thread in destroy() %d", ret);
1093 static void hwc_destroy_display(struct hwc_drm_display *hd)
1097 if (hwc_destroy_worker(&hd->set_worker))
1098 ALOGE("Destroy set worker failed");
1101 static int hwc_device_close(struct hw_device_t *dev)
1103 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
1106 for (i = 0; i < MAX_NUM_DISPLAYS; i++)
1107 hwc_destroy_display(&ctx->displays[i]);
1109 if (hwc_destroy_worker(&ctx->event_worker))
1110 ALOGE("Destroy event worker failed");
1114 ret = hwc_import_destroy(ctx->import_ctx);
1116 ALOGE("Could not destroy import %d", ret);
1123 static int hwc_initialize_worker(struct hwc_worker *worker,
1124 void *(*routine)(void*), void *arg)
1128 ret = pthread_cond_init(&worker->cond, NULL);
1130 ALOGE("Failed to create worker condition %d", ret);
1134 ret = pthread_mutex_init(&worker->lock, NULL);
1136 ALOGE("Failed to initialize worker lock %d", ret);
1140 worker->exit = false;
1142 ret = pthread_create(&worker->thread, NULL, routine, arg);
1144 ALOGE("Could not create worker thread %d", ret);
1150 pthread_mutex_destroy(&worker->lock);
1152 pthread_cond_destroy(&worker->cond);
1157 * TODO: This function sets the active config to the first one in the list. This
1158 * should be fixed such that it selects the preferred mode for the display, or
1159 * some other, saner, method of choosing the config.
1161 static int hwc_set_initial_config(struct hwc_drm_display *hd)
1165 size_t num_configs = 1;
1167 ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1169 if (ret || !num_configs)
1172 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1174 ALOGE("Failed to set active config d=%d ret=%d", hd->display,
1182 static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1183 uint32_t connector_id)
1185 struct hwc_drm_display *hd = NULL;
1188 ret = hwc_get_drm_display(ctx, display, &hd);
1193 hd->display = display;
1194 hd->active_pipe = -1;
1195 hd->initial_modeset_required = true;
1196 hd->connector_id = connector_id;
1197 hd->enable_vsync_events = false;
1198 hd->vsync_sequence = 0;
1200 ret = pthread_mutex_init(&hd->flip_lock, NULL);
1202 ALOGE("Failed to initialize flip lock %d", ret);
1206 ret = pthread_cond_init(&hd->flip_cond, NULL);
1208 ALOGE("Failed to intiialize flip condition %d", ret);
1212 ret = sw_sync_timeline_create();
1214 ALOGE("Failed to create sw sync timeline %d", ret);
1217 hd->timeline_fd = ret;
1220 * Initialize timeline_next to 1, because point 0 will be the very first
1221 * set operation. Since we increment every time set() is called,
1222 * initializing to 0 would cause an off-by-one error where
1223 * surfaceflinger would composite on the front buffer.
1225 hd->timeline_next = 1;
1227 ret = hwc_set_initial_config(hd);
1229 ALOGE("Failed to set initial config for d=%d ret=%d", display,
1231 goto err_sync_timeline;
1234 ret = hwc_initialize_worker(&hd->set_worker, hwc_set_worker, hd);
1236 ALOGE("Failed to create set worker %d\n", ret);
1237 goto err_sync_timeline;
1243 close(hd->timeline_fd);
1246 pthread_cond_destroy(&hd->flip_cond);
1249 pthread_mutex_destroy(&hd->flip_lock);
1254 static int hwc_enumerate_displays(struct hwc_context_t *ctx)
1256 struct hwc_drm_display *panel_hd;
1258 drmModeConnectorPtr *conn_list;
1261 ret = hwc_initialize_worker(&ctx->event_worker, hwc_event_worker, ctx);
1263 ALOGE("Failed to create event worker %d\n", ret);
1267 res = drmModeGetResources(ctx->fd);
1269 ALOGE("Failed to get drm resources");
1273 conn_list = (drmModeConnector **)calloc(res->count_connectors,
1274 sizeof(*conn_list));
1276 ALOGE("Failed to allocate connector list");
1281 for (i = 0; i < res->count_connectors; i++) {
1282 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1283 if (!conn_list[i]) {
1284 ALOGE("Failed to get connector %d", res->connectors[i]);
1290 ctx->num_displays = 0;
1292 /* Find a connected, panel type connector for display 0 */
1293 for (i = 0; i < res->count_connectors; i++) {
1294 drmModeConnectorPtr c = conn_list[i];
1296 for (j = 0; j < ARRAY_SIZE(panel_types); j++) {
1297 if (c->connector_type == panel_types[j] &&
1298 c->connection == DRM_MODE_CONNECTED)
1301 if (j == ARRAY_SIZE(panel_types))
1304 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1305 ctx->num_displays++;
1309 ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1313 /* Fill in the other displays */
1314 for (i = 0; i < res->count_connectors; i++) {
1315 drmModeConnectorPtr c = conn_list[i];
1317 if (panel_hd->connector_id == c->connector_id)
1320 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1321 ctx->num_displays++;
1325 for (i = 0; i < res->count_connectors; i++) {
1327 drmModeFreeConnector(conn_list[i]);
1332 drmModeFreeResources(res);
1335 hwc_destroy_worker(&ctx->event_worker);
1340 static int hwc_device_open(const struct hw_module_t* module, const char* name,
1341 struct hw_device_t** dev)
1344 struct hwc_context_t *ctx;
1345 char path[PROPERTY_VALUE_MAX];
1347 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1348 ALOGE("Invalid module name- %s", name);
1352 ctx = new hwc_context_t();
1354 ALOGE("Failed to allocate hwc context");
1358 ret = hwc_import_init(&ctx->import_ctx);
1360 ALOGE("Failed to initialize import context");
1364 property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE);
1365 /* TODO: Use drmOpenControl here instead */
1366 ctx->fd = open(path, O_RDWR);
1368 ALOGE("Failed to open dri- %s", strerror(-errno));
1372 ret = hwc_enumerate_displays(ctx);
1374 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1378 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1379 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1380 ctx->device.common.module = const_cast<hw_module_t*>(module);
1381 ctx->device.common.close = hwc_device_close;
1383 ctx->device.prepare = hwc_prepare;
1384 ctx->device.set = hwc_set;
1385 ctx->device.eventControl = hwc_event_control;
1386 ctx->device.setPowerMode = hwc_set_power_mode;
1387 ctx->device.query = hwc_query;
1388 ctx->device.registerProcs = hwc_register_procs;
1389 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1390 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1391 ctx->device.getActiveConfig = hwc_get_active_config;
1392 ctx->device.setActiveConfig = hwc_set_active_config;
1393 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1395 *dev = &ctx->device.common;
1406 static struct hw_module_methods_t hwc_module_methods = {
1407 open: hwc_device_open
1410 hwc_module_t HAL_MODULE_INFO_SYM = {
1412 tag: HARDWARE_MODULE_TAG,
1415 id: HWC_HARDWARE_MODULE_ID,
1416 name: "DRM hwcomposer module",
1417 author: "The Android Open Source Project",
1418 methods: &hwc_module_methods,