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"
19 #include "drm_hwcomposer.h"
25 #include <sys/param.h>
26 #include <sys/resource.h>
31 #include <xf86drmMode.h>
33 #include <cutils/log.h>
34 #include <cutils/properties.h>
35 #include <hardware/hardware.h>
36 #include <hardware/hwcomposer.h>
38 #include <sync/sync.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, DRM_MODE_CONNECTOR_eDP, DRM_MODE_CONNECTOR_DSI,
57 struct hwc_drm_display {
58 struct hwc_context_t *ctx;
61 uint32_t connector_id;
63 drmModeModeInfoPtr configs;
66 drmModeModeInfo active_mode;
69 bool initial_modeset_required;
71 struct hwc_worker set_worker;
73 std::list<struct hwc_drm_bo> buf_queue;
74 struct hwc_drm_bo front;
75 pthread_mutex_t flip_lock;
76 pthread_cond_t flip_cond;
79 unsigned timeline_next;
81 bool enable_vsync_events;
82 unsigned int vsync_sequence;
85 struct hwc_context_t {
86 hwc_composer_device_1_t device;
90 hwc_procs_t const *procs;
91 struct hwc_import_context *import_ctx;
93 struct hwc_drm_display displays[MAX_NUM_DISPLAYS];
96 struct hwc_worker event_worker;
99 static int hwc_get_drm_display(struct hwc_context_t *ctx, int display,
100 struct hwc_drm_display **hd) {
101 if (display >= MAX_NUM_DISPLAYS) {
102 ALOGE("Requested display is out-of-bounds %d %d", display,
106 *hd = &ctx->displays[display];
110 static int hwc_prepare_layer(hwc_layer_1_t *layer) {
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");
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");
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");
129 /* TODO: Handle transformations */
130 if (layer->transform) {
131 ALOGV("Can't handle transformations yet");
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");
140 /* TODO: Handle cropping & scaling */
145 static int hwc_prepare(hwc_composer_device_1_t * /* dev */, size_t num_displays,
146 hwc_display_contents_1_t **display_contents) {
147 /* TODO: Check flags for HWC_GEOMETRY_CHANGED */
149 for (int i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; ++i) {
150 if (!display_contents[i])
153 for (int j = 0; j < (int)display_contents[i]->numHwLayers; ++j) {
154 int ret = hwc_prepare_layer(&display_contents[i]->hwLayers[j]);
156 ALOGE("Failed to prepare layer %d:%d", j, i);
165 static int hwc_queue_vblank_event(struct hwc_drm_display *hd) {
166 if (hd->active_pipe == -1) {
167 ALOGE("Active pipe is -1 disp=%d", hd->display);
172 memset(&vblank, 0, sizeof(vblank));
174 uint32_t high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
175 vblank.request.type = (drmVBlankSeqType)(
176 DRM_VBLANK_ABSOLUTE | DRM_VBLANK_NEXTONMISS | DRM_VBLANK_EVENT |
177 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
178 vblank.request.signal = (unsigned long)hd;
179 vblank.request.sequence = hd->vsync_sequence + 1;
181 int ret = drmWaitVBlank(hd->ctx->fd, &vblank);
183 ALOGE("Failed to wait for vblank %d", ret);
190 static void hwc_vblank_event_handler(int /* fd */, unsigned int sequence,
191 unsigned int tv_sec, unsigned int tv_usec,
193 struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data;
195 if (!hd->enable_vsync_events || !hd->ctx->procs->vsync)
199 * Discard duplicate vsync (can happen when enabling vsync events while
200 * already processing vsyncs).
202 if (sequence <= hd->vsync_sequence)
205 hd->vsync_sequence = sequence;
206 int ret = hwc_queue_vblank_event(hd);
208 ALOGE("Failed to queue vblank event ret=%d", ret);
211 (int64_t)tv_sec * 1000 * 1000 * 1000 + (int64_t)tv_usec * 1000;
212 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
215 static void hwc_flip_event_handler(int /* fd */, unsigned int /* sequence */,
216 unsigned int /* tv_sec */,
217 unsigned int /* tv_usec */,
219 struct hwc_drm_display *hd = (struct hwc_drm_display *)user_data;
221 int ret = pthread_mutex_lock(&hd->flip_lock);
223 ALOGE("Failed to lock flip lock ret=%d", ret);
227 ret = pthread_cond_signal(&hd->flip_cond);
229 ALOGE("Failed to signal flip condition ret=%d", ret);
231 ret = pthread_mutex_unlock(&hd->flip_lock);
233 ALOGE("Failed to unlock flip lock ret=%d", ret);
238 static void *hwc_event_worker(void *arg) {
239 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
241 struct hwc_context_t *ctx = (struct hwc_context_t *)arg;
245 FD_SET(ctx->fd, &fds);
247 drmEventContext event_context;
248 event_context.version = DRM_EVENT_CONTEXT_VERSION;
249 event_context.page_flip_handler = hwc_flip_event_handler;
250 event_context.vblank_handler = hwc_vblank_event_handler;
254 ret = select(ctx->fd + 1, &fds, NULL, NULL, NULL);
255 } while (ret == -1 && errno == EINTR);
258 ALOGE("Failed waiting for drm event\n");
262 drmHandleEvent(ctx->fd, &event_context);
268 static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b) {
269 return a->clock == b->clock && a->hdisplay == b->hdisplay &&
270 a->hsync_start == b->hsync_start && a->hsync_end == b->hsync_end &&
271 a->htotal == b->htotal && a->hskew == b->hskew &&
272 a->vdisplay == b->vdisplay && a->vsync_start == b->vsync_start &&
273 a->vsync_end == b->vsync_end && a->vtotal == b->vtotal &&
274 a->vscan == b->vscan && a->vrefresh == b->vrefresh &&
275 a->flags == b->flags && a->type == b->type &&
276 !strcmp(a->name, b->name);
279 static int hwc_modeset_required(struct hwc_drm_display *hd,
280 bool *modeset_required) {
281 if (hd->initial_modeset_required) {
282 *modeset_required = true;
283 hd->initial_modeset_required = false;
288 crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc);
290 ALOGE("Failed to get crtc for display %d", hd->display);
294 drmModeModeInfoPtr m;
295 m = &hd->active_mode;
297 /* Do a modeset if we haven't done one, or the mode has changed */
298 if (!crtc->mode_valid || !hwc_mode_is_equal(m, &crtc->mode))
299 *modeset_required = true;
301 *modeset_required = false;
303 drmModeFreeCrtc(crtc);
308 static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf) {
309 bool modeset_required;
310 int ret = hwc_modeset_required(hd, &modeset_required);
312 ALOGE("Failed to determine if modeset is required %d", ret);
315 if (modeset_required) {
316 ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id, 0, 0,
317 &hd->connector_id, 1, &hd->active_mode);
319 ALOGE("Modeset failed for crtc %d", hd->active_crtc);
325 ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id,
326 DRM_MODE_PAGE_FLIP_EVENT, hd);
328 ALOGE("Failed to flip buffer for crtc %d", hd->active_crtc);
332 ret = pthread_cond_wait(&hd->flip_cond, &hd->flip_lock);
334 ALOGE("Failed to wait on condition %d", ret);
341 static int hwc_wait_and_set(struct hwc_drm_display *hd,
342 struct hwc_drm_bo *buf) {
344 if (buf->acquire_fence_fd >= 0) {
345 ret = sync_wait(buf->acquire_fence_fd, -1);
346 close(buf->acquire_fence_fd);
347 buf->acquire_fence_fd = -1;
349 ALOGE("Failed to wait for acquire %d", ret);
354 ret = hwc_flip(hd, buf);
356 ALOGE("Failed to perform flip\n");
360 if (hwc_import_bo_release(hd->ctx->fd, hd->ctx->import_ctx, &hd->front)) {
361 struct drm_gem_close args;
362 memset(&args, 0, sizeof(args));
363 for (int i = 0; i < ARRAY_SIZE(hd->front.gem_handles); ++i) {
364 if (!hd->front.gem_handles[i])
367 ret = pthread_mutex_lock(&hd->set_worker.lock);
369 ALOGE("Failed to lock set lock in wait_and_set() %d", ret);
373 /* check for duplicate handle in buf_queue */
375 for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin();
376 bi != hd->buf_queue.end(); ++bi)
377 for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); ++j)
378 if (hd->front.gem_handles[i] == bi->gem_handles[j])
381 for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); ++j)
382 if (hd->front.gem_handles[i] == buf->gem_handles[j])
386 args.handle = hd->front.gem_handles[i];
387 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
389 if (pthread_mutex_unlock(&hd->set_worker.lock))
390 ALOGE("Failed to unlock set lock in wait_and_set() %d", ret);
399 static void *hwc_set_worker(void *arg) {
400 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
402 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
403 int ret = pthread_mutex_lock(&hd->flip_lock);
405 ALOGE("Failed to lock flip lock ret=%d", ret);
410 ret = pthread_mutex_lock(&hd->set_worker.lock);
412 ALOGE("Failed to lock set lock %d", ret);
416 if (hd->set_worker.exit)
419 if (hd->buf_queue.empty()) {
420 ret = pthread_cond_wait(&hd->set_worker.cond, &hd->set_worker.lock);
422 ALOGE("Failed to wait on condition %d", ret);
427 struct hwc_drm_bo buf;
428 buf = hd->buf_queue.front();
429 hd->buf_queue.pop_front();
431 ret = pthread_mutex_unlock(&hd->set_worker.lock);
433 ALOGE("Failed to unlock set lock %d", ret);
437 ret = hwc_wait_and_set(hd, &buf);
439 ALOGE("Failed to wait and set %d", ret);
441 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
443 ALOGE("Failed to increment sync timeline %d", ret);
446 ret = pthread_mutex_unlock(&hd->set_worker.lock);
448 ALOGE("Failed to unlock set lock while exiting %d", ret);
450 ret = pthread_mutex_unlock(&hd->flip_lock);
452 ALOGE("Failed to unlock flip lock ret=%d", ret);
457 static void hwc_close_fences(hwc_display_contents_1_t *display_contents) {
458 for (int i = 0; i < (int)display_contents->numHwLayers; ++i) {
459 hwc_layer_1_t *layer = &display_contents->hwLayers[i];
460 if (layer->acquireFenceFd >= 0) {
461 close(layer->acquireFenceFd);
462 layer->acquireFenceFd = -1;
465 if (display_contents->outbufAcquireFenceFd >= 0) {
466 close(display_contents->outbufAcquireFenceFd);
467 display_contents->outbufAcquireFenceFd = -1;
471 static int hwc_set_display(hwc_context_t *ctx, int display,
472 hwc_display_contents_1_t *display_contents) {
473 struct hwc_drm_display *hd = NULL;
474 int ret = hwc_get_drm_display(ctx, display, &hd);
476 hwc_close_fences(display_contents);
480 if (!hd->active_crtc) {
481 ALOGE("There is no active crtc for display %d", display);
482 hwc_close_fences(display_contents);
487 * TODO: We can only support one hw layer atm, so choose either the
488 * first one or the framebuffer target.
490 hwc_layer_1_t *layer = NULL;
491 if (!display_contents->numHwLayers) {
493 } else if (display_contents->numHwLayers == 1) {
494 layer = &display_contents->hwLayers[0];
497 for (i = 0; i < (int)display_contents->numHwLayers; ++i) {
498 layer = &display_contents->hwLayers[i];
499 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
502 if (i == (int)display_contents->numHwLayers) {
503 ALOGE("Could not find a suitable layer for display %d", display);
507 ret = pthread_mutex_lock(&hd->set_worker.lock);
509 ALOGE("Failed to lock set lock in set() %d", ret);
510 hwc_close_fences(display_contents);
514 struct hwc_drm_bo buf;
515 memset(&buf, 0, sizeof(buf));
516 ret = hwc_import_bo_create(ctx->fd, ctx->import_ctx, layer->handle, &buf);
518 ALOGE("Failed to import handle to drm bo %d", ret);
519 hwc_close_fences(display_contents);
522 buf.acquire_fence_fd = layer->acquireFenceFd;
523 layer->acquireFenceFd = -1;
526 * TODO: Retire and release can use the same sync point here b/c hwc is
527 * restricted to one layer. Once that is no longer true, this will need
531 display_contents->retireFenceFd = sw_sync_fence_create(
532 hd->timeline_fd, "drm_hwc_retire", hd->timeline_next);
533 layer->releaseFenceFd = sw_sync_fence_create(
534 hd->timeline_fd, "drm_hwc_release", hd->timeline_next);
535 hd->buf_queue.push_back(buf);
537 ret = pthread_cond_signal(&hd->set_worker.cond);
539 ALOGE("Failed to signal set worker %d", ret);
541 if (pthread_mutex_unlock(&hd->set_worker.lock))
542 ALOGE("Failed to unlock set lock in set()");
544 hwc_close_fences(display_contents);
548 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
549 hwc_display_contents_1_t **display_contents) {
550 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
553 for (int i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; ++i) {
554 if (display_contents[i])
555 ret = hwc_set_display(ctx, i, display_contents[i]);
561 static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
562 int event, int enabled) {
563 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
564 struct hwc_drm_display *hd = NULL;
565 int ret = hwc_get_drm_display(ctx, display, &hd);
569 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
572 if (hd->active_pipe == -1) {
573 ALOGD("Can't service events for display %d, no pipe", display);
577 hd->enable_vsync_events = !!enabled;
579 if (!hd->enable_vsync_events)
583 * Note that it's possible that the event worker is already waiting for
584 * a vsync, and this will be a duplicate request. In that event, we'll
585 * end up firing the event handler twice, and it will discard the second
586 * event. Not ideal, but not worth introducing a bunch of additional
587 * logic/locks/state for.
589 ret = hwc_queue_vblank_event(hd);
591 ALOGE("Failed to queue vblank event ret=%d", ret);
598 static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
600 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
602 struct hwc_drm_display *hd = NULL;
603 int ret = hwc_get_drm_display(ctx, display, &hd);
607 drmModeConnectorPtr c = drmModeGetConnector(ctx->fd, hd->connector_id);
609 ALOGE("Failed to get connector %d", display);
613 uint32_t dpms_prop = 0;
614 for (int i = 0; !dpms_prop && i < c->count_props; ++i) {
615 drmModePropertyPtr p;
617 p = drmModeGetProperty(ctx->fd, c->props[i]);
621 if (!strcmp(p->name, "DPMS"))
622 dpms_prop = c->props[i];
624 drmModeFreeProperty(p);
627 ALOGE("Failed to get DPMS property from display %d", display);
628 drmModeFreeConnector(c);
632 uint64_t dpms_value = 0;
634 case HWC_POWER_MODE_OFF:
635 dpms_value = DRM_MODE_DPMS_OFF;
638 /* We can't support dozing right now, so go full on */
639 case HWC_POWER_MODE_DOZE:
640 case HWC_POWER_MODE_DOZE_SUSPEND:
641 case HWC_POWER_MODE_NORMAL:
642 dpms_value = DRM_MODE_DPMS_ON;
646 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id, dpms_prop,
649 ALOGE("Failed to set DPMS property for display %d", display);
650 drmModeFreeConnector(c);
654 drmModeFreeConnector(c);
658 static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
661 case HWC_BACKGROUND_LAYER_SUPPORTED:
662 *value = 0; /* TODO: We should do this */
664 case HWC_VSYNC_PERIOD:
665 ALOGW("Query for deprecated vsync value, returning 60Hz");
666 *value = 1000 * 1000 * 1000 / 60;
668 case HWC_DISPLAY_TYPES_SUPPORTED:
669 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
675 static void hwc_register_procs(struct hwc_composer_device_1 *dev,
676 hwc_procs_t const *procs) {
677 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
682 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
683 int display, uint32_t *configs,
684 size_t *numConfigs) {
688 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
689 struct hwc_drm_display *hd = NULL;
690 int ret = hwc_get_drm_display(ctx, display, &hd);
694 drmModeConnectorPtr c = drmModeGetConnector(ctx->fd, hd->connector_id);
696 ALOGE("Failed to get connector %d", display);
705 if (c->connection == DRM_MODE_DISCONNECTED) {
706 drmModeFreeConnector(c);
711 (drmModeModeInfoPtr)calloc(c->count_modes, sizeof(*hd->configs));
713 ALOGE("Failed to allocate config list for display %d", display);
715 drmModeFreeConnector(c);
719 for (int i = 0; i < c->count_modes; ++i) {
720 drmModeModeInfoPtr m = &hd->configs[i];
722 memcpy(m, &c->modes[i], sizeof(*m));
724 if (i < (int)*numConfigs)
728 hd->num_configs = c->count_modes;
729 *numConfigs = MIN(c->count_modes, *numConfigs);
731 drmModeFreeConnector(c);
735 static int hwc_check_config_valid(struct hwc_context_t *ctx,
736 drmModeConnectorPtr connector, int display,
738 struct hwc_drm_display *hd = NULL;
739 int ret = hwc_get_drm_display(ctx, display, &hd);
743 /* Make sure the requested config is still valid for the display */
744 drmModeModeInfoPtr m = NULL;
745 for (int i = 0; i < connector->count_modes; ++i) {
746 if (hwc_mode_is_equal(&connector->modes[i], &hd->configs[config_idx])) {
747 m = &hd->configs[config_idx];
757 static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
758 int display, uint32_t config,
759 const uint32_t *attributes,
761 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
762 struct hwc_drm_display *hd = NULL;
763 int ret = hwc_get_drm_display(ctx, display, &hd);
767 if (config >= hd->num_configs) {
768 ALOGE("Requested config is out-of-bounds %d %d", config, hd->num_configs);
772 drmModeConnectorPtr c = drmModeGetConnector(ctx->fd, hd->connector_id);
774 ALOGE("Failed to get connector %d", display);
778 ret = hwc_check_config_valid(ctx, c, display, (int)config);
780 ALOGE("Provided config is no longer valid %u", config);
781 drmModeFreeConnector(c);
785 drmModeModeInfoPtr m = &hd->configs[config];
786 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
787 switch (attributes[i]) {
788 case HWC_DISPLAY_VSYNC_PERIOD:
789 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
791 case HWC_DISPLAY_WIDTH:
792 values[i] = m->hdisplay;
794 case HWC_DISPLAY_HEIGHT:
795 values[i] = m->vdisplay;
797 case HWC_DISPLAY_DPI_X:
798 /* Dots per 1000 inches */
799 values[i] = c->mmWidth ? (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
801 case HWC_DISPLAY_DPI_Y:
802 /* Dots per 1000 inches */
803 values[i] = c->mmHeight ? (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
808 drmModeFreeConnector(c);
812 static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
814 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
815 struct hwc_drm_display *hd = NULL;
816 int ret = hwc_get_drm_display(ctx, display, &hd);
820 /* Find the current mode in the config list */
822 for (int i = 0; i < (int)hd->num_configs; ++i) {
823 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
831 static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id) {
832 for (int i = 0; i < MAX_NUM_DISPLAYS; ++i) {
833 if (ctx->displays[i].active_crtc == crtc_id)
839 static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
840 uint32_t encoder_id, uint32_t *crtc_id) {
841 drmModeEncoderPtr e = drmModeGetEncoder(ctx->fd, encoder_id);
843 ALOGE("Failed to get encoder for connector %d", encoder_id);
847 /* First try to use the currently-bound crtc */
850 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
851 *crtc_id = e->crtc_id;
852 drmModeFreeEncoder(e);
857 /* Try to find a possible crtc which will work */
858 for (int i = 0; i < r->count_crtcs; ++i) {
859 if (!(e->possible_crtcs & (1 << i)))
862 /* We've already tried this earlier */
863 if (e->crtc_id == r->crtcs[i])
866 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
867 *crtc_id = r->crtcs[i];
868 drmModeFreeEncoder(e);
873 /* We can't use the encoder, but nothing went wrong, try another one */
874 drmModeFreeEncoder(e);
878 static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
880 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
881 struct hwc_drm_display *hd = NULL;
882 int ret = hwc_get_drm_display(ctx, display, &hd);
886 drmModeConnectorPtr c = drmModeGetConnector(ctx->fd, hd->connector_id);
888 ALOGE("Failed to get connector %d", display);
892 if (c->connection == DRM_MODE_DISCONNECTED) {
893 ALOGE("Tried to configure a disconnected display %d", display);
894 drmModeFreeConnector(c);
898 if (index >= c->count_modes) {
899 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
900 drmModeFreeConnector(c);
904 drmModeResPtr r = drmModeGetResources(ctx->fd);
906 ALOGE("Failed to get drm resources");
907 drmModeFreeResources(r);
908 drmModeFreeConnector(c);
912 /* We no longer have an active_crtc */
914 hd->active_pipe = -1;
916 /* First, try to use the currently-connected encoder */
917 uint32_t crtc_id = 0;
919 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
920 if (ret && ret != -EAGAIN) {
921 ALOGE("Encoder try failed %d", ret);
922 drmModeFreeResources(r);
923 drmModeFreeConnector(c);
928 /* We couldn't find a crtc with the attached encoder, try the others */
930 for (int i = 0; i < c->count_encoders; ++i) {
931 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
934 } else if (ret != -EAGAIN) {
935 ALOGE("Encoder try failed %d", ret);
936 drmModeFreeResources(r);
937 drmModeFreeConnector(c);
942 ALOGE("Couldn't find valid crtc to modeset");
943 drmModeFreeConnector(c);
944 drmModeFreeResources(r);
948 drmModeFreeConnector(c);
950 hd->active_crtc = crtc_id;
951 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
953 /* Find the pipe corresponding to the crtc_id */
954 for (int i = 0; i < r->count_crtcs; ++i) {
955 /* We've already tried this earlier */
956 if (r->crtcs[i] == crtc_id) {
961 drmModeFreeResources(r);
962 /* This should never happen... hehehe */
963 if (hd->active_pipe == -1) {
964 ALOGE("Active crtc was not found in resources!!");
968 /* TODO: Once we have atomic, set the crtc timing info here */
972 static int hwc_destroy_worker(struct hwc_worker *worker) {
973 int ret = pthread_mutex_lock(&worker->lock);
975 ALOGE("Failed to lock in destroy() %d", ret);
981 ret |= pthread_cond_signal(&worker->cond);
983 ALOGE("Failed to signal cond in destroy() %d", ret);
985 ret |= pthread_mutex_unlock(&worker->lock);
987 ALOGE("Failed to unlock in destroy() %d", ret);
989 ret |= pthread_join(worker->thread, NULL);
990 if (ret && ret != ESRCH)
991 ALOGE("Failed to join thread in destroy() %d", ret);
996 static void hwc_destroy_display(struct hwc_drm_display *hd) {
997 if (hwc_destroy_worker(&hd->set_worker))
998 ALOGE("Destroy set worker failed");
1001 static int hwc_device_close(struct hw_device_t *dev) {
1002 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
1004 for (int i = 0; i < MAX_NUM_DISPLAYS; ++i)
1005 hwc_destroy_display(&ctx->displays[i]);
1007 if (hwc_destroy_worker(&ctx->event_worker))
1008 ALOGE("Destroy event worker failed");
1012 int ret = hwc_import_destroy(ctx->import_ctx);
1014 ALOGE("Could not destroy import %d", ret);
1021 static int hwc_initialize_worker(struct hwc_worker *worker,
1022 void *(*routine)(void *), void *arg) {
1023 int ret = pthread_cond_init(&worker->cond, NULL);
1025 ALOGE("Failed to create worker condition %d", ret);
1029 ret = pthread_mutex_init(&worker->lock, NULL);
1031 ALOGE("Failed to initialize worker lock %d", ret);
1032 pthread_cond_destroy(&worker->cond);
1036 worker->exit = false;
1038 ret = pthread_create(&worker->thread, NULL, routine, arg);
1040 ALOGE("Could not create worker thread %d", ret);
1041 pthread_mutex_destroy(&worker->lock);
1042 pthread_cond_destroy(&worker->cond);
1049 * TODO: This function sets the active config to the first one in the list. This
1050 * should be fixed such that it selects the preferred mode for the display, or
1051 * some other, saner, method of choosing the config.
1053 static int hwc_set_initial_config(struct hwc_drm_display *hd) {
1055 size_t num_configs = 1;
1056 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1058 if (ret || !num_configs)
1061 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1063 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
1070 static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1071 uint32_t connector_id) {
1072 struct hwc_drm_display *hd = NULL;
1073 int ret = hwc_get_drm_display(ctx, display, &hd);
1078 hd->display = display;
1079 hd->active_pipe = -1;
1080 hd->initial_modeset_required = true;
1081 hd->connector_id = connector_id;
1082 hd->enable_vsync_events = false;
1083 hd->vsync_sequence = 0;
1085 ret = pthread_mutex_init(&hd->flip_lock, NULL);
1087 ALOGE("Failed to initialize flip lock %d", ret);
1091 ret = pthread_cond_init(&hd->flip_cond, NULL);
1093 ALOGE("Failed to intiialize flip condition %d", ret);
1094 pthread_mutex_destroy(&hd->flip_lock);
1098 ret = sw_sync_timeline_create();
1100 ALOGE("Failed to create sw sync timeline %d", ret);
1101 pthread_cond_destroy(&hd->flip_cond);
1102 pthread_mutex_destroy(&hd->flip_lock);
1105 hd->timeline_fd = ret;
1108 * Initialize timeline_next to 1, because point 0 will be the very first
1109 * set operation. Since we increment every time set() is called,
1110 * initializing to 0 would cause an off-by-one error where
1111 * surfaceflinger would composite on the front buffer.
1113 hd->timeline_next = 1;
1115 ret = hwc_set_initial_config(hd);
1117 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
1118 close(hd->timeline_fd);
1119 pthread_cond_destroy(&hd->flip_cond);
1120 pthread_mutex_destroy(&hd->flip_lock);
1124 ret = hwc_initialize_worker(&hd->set_worker, hwc_set_worker, hd);
1126 ALOGE("Failed to create set worker %d\n", ret);
1127 close(hd->timeline_fd);
1128 pthread_cond_destroy(&hd->flip_cond);
1129 pthread_mutex_destroy(&hd->flip_lock);
1136 static void hwc_free_conn_list(drmModeConnectorPtr *conn_list, int num_conn) {
1137 for (int i = 0; i < num_conn; ++i) {
1139 drmModeFreeConnector(conn_list[i]);
1144 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
1145 drmModeResPtr res = drmModeGetResources(ctx->fd);
1147 ALOGE("Failed to get drm resources");
1150 int num_connectors = res->count_connectors;
1152 drmModeConnectorPtr *conn_list =
1153 (drmModeConnector **)calloc(num_connectors, sizeof(*conn_list));
1155 ALOGE("Failed to allocate connector list");
1156 drmModeFreeResources(res);
1160 for (int i = 0; i < num_connectors; ++i) {
1161 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1162 if (!conn_list[i]) {
1163 ALOGE("Failed to get connector %d", res->connectors[i]);
1164 drmModeFreeResources(res);
1168 drmModeFreeResources(res);
1170 ctx->num_displays = 0;
1172 /* Find a connected, panel type connector for display 0 */
1173 for (int i = 0; i < num_connectors; ++i) {
1174 drmModeConnectorPtr c = conn_list[i];
1177 for (j = 0; j < ARRAY_SIZE(panel_types); ++j) {
1178 if (c->connector_type == panel_types[j] &&
1179 c->connection == DRM_MODE_CONNECTED)
1182 if (j == ARRAY_SIZE(panel_types))
1185 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1186 ++ctx->num_displays;
1190 struct hwc_drm_display *panel_hd;
1191 int ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1193 hwc_free_conn_list(conn_list, num_connectors);
1197 /* Fill in the other displays */
1198 for (int i = 0; i < num_connectors; ++i) {
1199 drmModeConnectorPtr c = conn_list[i];
1201 if (panel_hd->connector_id == c->connector_id)
1204 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1205 ++ctx->num_displays;
1207 hwc_free_conn_list(conn_list, num_connectors);
1209 ret = hwc_initialize_worker(&ctx->event_worker, hwc_event_worker, ctx);
1211 ALOGE("Failed to create event worker %d\n", ret);
1218 static int hwc_device_open(const struct hw_module_t *module, const char *name,
1219 struct hw_device_t **dev) {
1220 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1221 ALOGE("Invalid module name- %s", name);
1225 struct hwc_context_t *ctx = new hwc_context_t();
1227 ALOGE("Failed to allocate hwc context");
1231 int ret = hwc_import_init(&ctx->import_ctx);
1233 ALOGE("Failed to initialize import context");
1238 char path[PROPERTY_VALUE_MAX];
1239 property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE);
1240 /* TODO: Use drmOpenControl here instead */
1241 ctx->fd = open(path, O_RDWR);
1243 ALOGE("Failed to open dri- %s", strerror(-errno));
1248 ret = hwc_enumerate_displays(ctx);
1250 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1256 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1257 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1258 ctx->device.common.module = const_cast<hw_module_t *>(module);
1259 ctx->device.common.close = hwc_device_close;
1261 ctx->device.prepare = hwc_prepare;
1262 ctx->device.set = hwc_set;
1263 ctx->device.eventControl = hwc_event_control;
1264 ctx->device.setPowerMode = hwc_set_power_mode;
1265 ctx->device.query = hwc_query;
1266 ctx->device.registerProcs = hwc_register_procs;
1267 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1268 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1269 ctx->device.getActiveConfig = hwc_get_active_config;
1270 ctx->device.setActiveConfig = hwc_set_active_config;
1271 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1273 *dev = &ctx->device.common;
1278 static struct hw_module_methods_t hwc_module_methods = {open : hwc_device_open};
1280 hwc_module_t HAL_MODULE_INFO_SYM = {
1282 tag : HARDWARE_MODULE_TAG,
1285 id : HWC_HARDWARE_MODULE_ID,
1286 name : "DRM hwcomposer module",
1287 author : "The Android Open Source Project",
1288 methods : &hwc_module_methods,