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;
79 unsigned timeline_next;
81 struct hwc_worker vsync_worker;
82 bool enable_vsync_events;
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];
97 static int hwc_get_drm_display(struct hwc_context_t *ctx, int display,
98 struct hwc_drm_display **hd)
100 if (display >= MAX_NUM_DISPLAYS) {
101 ALOGE("Requested display is out-of-bounds %d %d", display,
105 *hd = &ctx->displays[display];
109 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)
150 /* TODO: Check flags for HWC_GEOMETRY_CHANGED */
152 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
154 if (!display_contents[i])
157 for (j = 0; j < (int)display_contents[i]->numHwLayers; j++) {
158 ret = hwc_prepare_layer(
159 &display_contents[i]->hwLayers[j]);
161 ALOGE("Failed to prepare layer %d:%d", j, i);
170 static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b)
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);
189 static int hwc_modeset_required(struct hwc_drm_display *hd,
190 bool *modeset_required)
193 drmModeModeInfoPtr m;
195 if (hd->initial_modeset_required) {
196 *modeset_required = true;
197 hd->initial_modeset_required = false;
201 crtc = drmModeGetCrtc(hd->ctx->fd, hd->active_crtc);
203 ALOGE("Failed to get crtc for display %d", hd->display);
207 m = &hd->active_mode;
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;
213 *modeset_required = false;
215 drmModeFreeCrtc(crtc);
220 static void hwc_flip_handler(int /* fd */, unsigned int /* sequence */,
221 unsigned int /* tv_sec */, unsigned int /* tv_usec */,
222 void */* user_data */)
226 static int hwc_flip(struct hwc_drm_display *hd, struct hwc_drm_bo *buf)
229 drmEventContext event_context;
231 bool modeset_required;
233 ret = hwc_modeset_required(hd, &modeset_required);
235 ALOGE("Failed to determine if modeset is required %d", ret);
238 if (modeset_required) {
239 ret = drmModeSetCrtc(hd->ctx->fd, hd->active_crtc, buf->fb_id,
240 0, 0, &hd->connector_id, 1,
243 ALOGE("Modeset failed for crtc %d",
251 FD_SET(hd->ctx->fd, &fds);
253 event_context.version = DRM_EVENT_CONTEXT_VERSION;
254 event_context.page_flip_handler = hwc_flip_handler;
256 ret = drmModePageFlip(hd->ctx->fd, hd->active_crtc, buf->fb_id,
257 DRM_MODE_PAGE_FLIP_EVENT, hd);
259 ALOGE("Failed to flip buffer for crtc %d",
265 ret = select(hd->ctx->fd + 1, &fds, NULL, NULL, NULL);
266 } while (ret == -1 && errno == EINTR);
269 ALOGE("Failed waiting for flip to complete\n");
272 drmHandleEvent(hd->ctx->fd, &event_context);
277 static int hwc_wait_and_set(struct hwc_drm_display *hd,
278 struct hwc_drm_bo *buf)
280 struct drm_gem_close args;
283 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;
288 ALOGE("Failed to wait for acquire %d", ret);
293 ret = drmModeAddFB2(hd->ctx->fd, buf->width,
294 buf->height, buf->format, buf->gem_handles, buf->pitches,
295 buf->offsets, &buf->fb_id, 0);
297 ALOGE("could not create drm fb %d", ret);
301 ret = hwc_flip(hd, buf);
303 ALOGE("Failed to perform flip\n");
307 if (hd->front.fb_id) {
308 ret = drmModeRmFB(hd->ctx->fd, hd->front.fb_id);
310 ALOGE("Failed to rm fb from front %d", ret);
315 memset(&args, 0, sizeof(args));
316 for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
317 if (!hd->front.gem_handles[i])
320 /* check for duplicate handle in buf_queue */
322 std::list<struct hwc_drm_bo>::iterator bi;
324 ret = pthread_mutex_lock(&hd->set_worker.lock);
326 ALOGE("Failed to lock set lock in wait_and_set() %d", ret);
331 for (bi = hd->buf_queue.begin();
332 bi != hd->buf_queue.end();
334 for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++)
335 if (hd->front.gem_handles[i] == bi->gem_handles[j])
339 args.handle = hd->front.gem_handles[i];
340 drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
342 if (pthread_mutex_unlock(&hd->set_worker.lock))
343 ALOGE("Failed to unlock set lock in wait_and_set() %d", ret);
350 static void *hwc_set_worker(void *arg)
352 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
355 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
358 struct hwc_drm_bo buf;
360 ret = pthread_mutex_lock(&hd->set_worker.lock);
362 ALOGE("Failed to lock set lock %d", ret);
366 if (hd->set_worker.exit)
369 if (hd->buf_queue.empty()) {
370 ret = pthread_cond_wait(&hd->set_worker.cond,
371 &hd->set_worker.lock);
373 ALOGE("Failed to wait on condition %d", ret);
378 buf = hd->buf_queue.front();
379 hd->buf_queue.pop_front();
381 ret = pthread_mutex_unlock(&hd->set_worker.lock);
383 ALOGE("Failed to unlock set lock %d", ret);
387 ret = hwc_wait_and_set(hd, &buf);
389 ALOGE("Failed to wait and set %d", ret);
391 ret = sw_sync_timeline_inc(hd->timeline_fd, 1);
393 ALOGE("Failed to increment sync timeline %d", ret);
397 ret = pthread_mutex_unlock(&hd->set_worker.lock);
399 ALOGE("Failed to unlock set lock while exiting %d", ret);
404 static int hwc_set_display(hwc_context_t *ctx, int display,
405 hwc_display_contents_1_t* display_contents)
407 struct hwc_drm_display *hd = NULL;
408 hwc_layer_1_t *layer = NULL;
409 struct hwc_drm_bo buf;
413 memset(&buf, 0, sizeof(buf));
415 ret = hwc_get_drm_display(ctx, display, &hd);
419 if (!hd->active_crtc) {
420 ALOGE("There is no active crtc for display %d", display);
426 * TODO: We can only support one hw layer atm, so choose either the
427 * first one or the framebuffer target.
429 if (!display_contents->numHwLayers) {
431 } else if (display_contents->numHwLayers == 1) {
432 layer = &display_contents->hwLayers[0];
434 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
435 layer = &display_contents->hwLayers[i];
436 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
439 if (i == (int)display_contents->numHwLayers) {
440 ALOGE("Could not find a suitable layer for display %d",
446 ret = pthread_mutex_lock(&hd->set_worker.lock);
448 ALOGE("Failed to lock set lock in set() %d", ret);
452 ret = hwc_create_bo_from_import(ctx->fd, ctx->import_ctx, layer->handle,
455 ALOGE("Failed to import handle to drm bo %d", ret);
458 buf.acquire_fence_fd = layer->acquireFenceFd;
459 layer->acquireFenceFd = -1;
462 * TODO: Retire and release can use the same sync point here b/c hwc is
463 * restricted to one layer. Once that is no longer true, this will need
467 display_contents->retireFenceFd = sw_sync_fence_create(hd->timeline_fd,
468 "drm_hwc_retire", hd->timeline_next);
469 layer->releaseFenceFd = sw_sync_fence_create(hd->timeline_fd,
470 "drm_hwc_release", hd->timeline_next);
471 hd->buf_queue.push_back(buf);
473 ret = pthread_cond_signal(&hd->set_worker.cond);
475 ALOGE("Failed to signal set worker %d", ret);
477 if (pthread_mutex_unlock(&hd->set_worker.lock))
478 ALOGE("Failed to unlock set lock in set()");
481 /* Close input fences. */
482 for (i = 0; i < (int)display_contents->numHwLayers; i++) {
483 layer = &display_contents->hwLayers[i];
484 if (layer->acquireFenceFd >= 0) {
485 close(layer->acquireFenceFd);
486 layer->acquireFenceFd = -1;
489 if (display_contents->outbufAcquireFenceFd >= 0) {
490 close(display_contents->outbufAcquireFenceFd);
491 display_contents->outbufAcquireFenceFd = -1;
497 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
498 hwc_display_contents_1_t** display_contents)
500 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
503 for (i = 0; i < (int)num_displays && i < MAX_NUM_DISPLAYS; i++) {
504 if (display_contents[i])
505 ret = hwc_set_display(ctx, i, display_contents[i]);
511 static int hwc_wait_for_vsync(struct hwc_drm_display *hd)
518 if (hd->active_pipe == -1)
521 memset(&vblank, 0, sizeof(vblank));
523 high_crtc = (hd->active_pipe << DRM_VBLANK_HIGH_CRTC_SHIFT);
524 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
525 (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
526 vblank.request.sequence = 1;
528 ret = drmWaitVBlank(hd->ctx->fd, &vblank);
530 ALOGE("Failed to wait for vblank %d", ret);
534 if (hd->ctx->procs->vsync) {
535 timestamp = vblank.reply.tval_sec * 1000 * 1000 * 1000 +
536 vblank.reply.tval_usec * 1000;
537 hd->ctx->procs->vsync(hd->ctx->procs, hd->display, timestamp);
543 static void *hwc_vsync_worker(void *arg)
545 struct hwc_drm_display *hd = (struct hwc_drm_display *)arg;
546 struct hwc_worker *w = &hd->vsync_worker;
549 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
552 ret = pthread_mutex_lock(&w->lock);
554 ALOGE("Failed to lock vsync lock %d", ret);
558 if (hd->active_pipe == -1) {
559 ALOGE("Pipe is no longer active, disable events");
560 hd->enable_vsync_events = false;
563 if (!hd->enable_vsync_events) {
564 ret = pthread_cond_wait(&w->cond, &w->lock);
566 ALOGE("Failed to wait on vsync cond %d", ret);
574 ret = pthread_mutex_unlock(&w->lock);
576 ALOGE("Failed to unlock vsync lock %d", ret);
580 if (!hd->enable_vsync_events)
583 ret = hwc_wait_for_vsync(hd);
585 ALOGE("Failed to wait for vsync %d", ret);
590 ret = pthread_mutex_unlock(&hd->set_worker.lock);
592 ALOGE("Failed to unlock set lock while exiting %d", ret);
597 static int hwc_event_control(struct hwc_composer_device_1* dev, int display,
598 int event, int enabled)
600 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
601 struct hwc_drm_display *hd = NULL;
604 ret = hwc_get_drm_display(ctx, display, &hd);
608 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
611 if (hd->active_pipe == -1) {
612 ALOGD("Can't service events for display %d, no pipe", display);
616 ret = pthread_mutex_lock(&hd->vsync_worker.lock);
618 ALOGE("Failed to lock vsync lock %d", ret);
622 hd->enable_vsync_events = !!enabled;
624 ret = pthread_cond_signal(&hd->vsync_worker.cond);
626 ALOGE("Failed to signal vsync thread %d", ret);
630 ret = pthread_mutex_unlock(&hd->vsync_worker.lock);
632 ALOGE("Failed to unlock vsync lock %d", ret);
639 if (pthread_mutex_unlock(&hd->vsync_worker.lock))
640 ALOGE("Failed to unlock vsync worker in error path");
645 static int hwc_set_power_mode(struct hwc_composer_device_1* dev, int display,
648 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
649 struct hwc_drm_display *hd = NULL;
650 drmModeConnectorPtr c;
652 uint32_t dpms_prop = 0;
653 uint64_t dpms_value = 0;
655 ret = hwc_get_drm_display(ctx, display, &hd);
659 c = drmModeGetConnector(ctx->fd, hd->connector_id);
661 ALOGE("Failed to get connector %d", display);
665 for (i = 0; !dpms_prop && i < c->count_props; i++) {
666 drmModePropertyPtr p;
668 p = drmModeGetProperty(ctx->fd, c->props[i]);
672 if (!strcmp(p->name, "DPMS"))
673 dpms_prop = c->props[i];
675 drmModeFreeProperty(p);
678 ALOGE("Failed to get DPMS property from display %d", display);
684 case HWC_POWER_MODE_OFF:
685 dpms_value = DRM_MODE_DPMS_OFF;
688 /* We can't support dozing right now, so go full on */
689 case HWC_POWER_MODE_DOZE:
690 case HWC_POWER_MODE_DOZE_SUSPEND:
691 case HWC_POWER_MODE_NORMAL:
692 dpms_value = DRM_MODE_DPMS_ON;
696 ret = drmModeConnectorSetProperty(ctx->fd, c->connector_id,
697 dpms_prop, dpms_value);
699 ALOGE("Failed to set DPMS property for display %d", display);
704 drmModeFreeConnector(c);
708 static int hwc_query(struct hwc_composer_device_1 */* dev */, int what,
712 case HWC_BACKGROUND_LAYER_SUPPORTED:
713 *value = 0; /* TODO: We should do this */
715 case HWC_VSYNC_PERIOD:
716 ALOGW("Query for deprecated vsync value, returning 60Hz");
717 *value = 1000 * 1000 * 1000 / 60;
719 case HWC_DISPLAY_TYPES_SUPPORTED:
720 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
726 static void hwc_register_procs(struct hwc_composer_device_1* dev,
727 hwc_procs_t const* procs)
729 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
734 static int hwc_get_display_configs(struct hwc_composer_device_1* dev,
735 int display, uint32_t* configs, size_t* numConfigs)
737 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
738 struct hwc_drm_display *hd = NULL;
739 drmModeConnectorPtr c;
745 ret = hwc_get_drm_display(ctx, display, &hd);
749 c = drmModeGetConnector(ctx->fd, hd->connector_id);
751 ALOGE("Failed to get connector %d", display);
760 if (c->connection == DRM_MODE_DISCONNECTED) {
765 hd->configs = (drmModeModeInfoPtr)calloc(c->count_modes,
766 sizeof(*hd->configs));
768 ALOGE("Failed to allocate config list for display %d", display);
774 for (i = 0; i < c->count_modes; i++) {
775 drmModeModeInfoPtr m = &hd->configs[i];
777 memcpy(m, &c->modes[i], sizeof(*m));
779 if (i < (int)*numConfigs)
783 hd->num_configs = c->count_modes;
784 *numConfigs = MIN(c->count_modes, *numConfigs);
787 drmModeFreeConnector(c);
791 static int hwc_check_config_valid(struct hwc_context_t *ctx,
792 drmModeConnectorPtr connector, int display,
795 struct hwc_drm_display *hd = NULL;
796 drmModeModeInfoPtr m = NULL;
799 ret = hwc_get_drm_display(ctx, display, &hd);
803 /* Make sure the requested config is still valid for the display */
804 for (i = 0; i < connector->count_modes; i++) {
805 if (hwc_mode_is_equal(&connector->modes[i],
806 &hd->configs[config_idx])) {
807 m = &hd->configs[config_idx];
817 static int hwc_get_display_attributes(struct hwc_composer_device_1* dev,
818 int display, uint32_t config, const uint32_t* attributes,
821 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
822 struct hwc_drm_display *hd = NULL;
823 drmModeConnectorPtr c;
824 drmModeModeInfoPtr m;
827 ret = hwc_get_drm_display(ctx, display, &hd);
831 if (config >= hd->num_configs) {
832 ALOGE("Requested config is out-of-bounds %d %d", config,
837 c = drmModeGetConnector(ctx->fd, hd->connector_id);
839 ALOGE("Failed to get connector %d", display);
843 ret = hwc_check_config_valid(ctx, c, display, (int)config);
845 ALOGE("Provided config is no longer valid %u", config);
849 m = &hd->configs[config];
850 for (i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
851 switch(attributes[i]) {
852 case HWC_DISPLAY_VSYNC_PERIOD:
853 values[i] = 1000 * 1000 * 1000 / m->vrefresh;
855 case HWC_DISPLAY_WIDTH:
856 values[i] = m->hdisplay;
858 case HWC_DISPLAY_HEIGHT:
859 values[i] = m->vdisplay;
861 case HWC_DISPLAY_DPI_X:
862 /* Dots per 1000 inches */
863 values[i] = c->mmWidth ?
864 (m->hdisplay * UM_PER_INCH) / c->mmWidth : 0;
866 case HWC_DISPLAY_DPI_Y:
867 /* Dots per 1000 inches */
868 values[i] = c->mmHeight ?
869 (m->vdisplay * UM_PER_INCH) / c->mmHeight : 0;
875 drmModeFreeConnector(c);
879 static int hwc_get_active_config(struct hwc_composer_device_1* dev, int display)
881 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
882 struct hwc_drm_display *hd = NULL;
883 int ret, i, index = -1;
885 ret = hwc_get_drm_display(ctx, display, &hd);
889 /* Find the current mode in the config list */
890 for (i = 0; i < (int)hd->num_configs; i++) {
891 if (hwc_mode_is_equal(&hd->configs[i], &hd->active_mode)) {
900 static bool hwc_crtc_is_bound(struct hwc_context_t *ctx, uint32_t crtc_id)
904 for (i = 0; i < MAX_NUM_DISPLAYS; i++) {
905 if (ctx->displays[i].active_crtc == crtc_id)
911 static int hwc_try_encoder(struct hwc_context_t *ctx, drmModeResPtr r,
912 uint32_t encoder_id, uint32_t *crtc_id)
917 e = drmModeGetEncoder(ctx->fd, encoder_id);
919 ALOGE("Failed to get encoder for connector %d", encoder_id);
923 /* First try to use the currently-bound crtc */
925 if (!hwc_crtc_is_bound(ctx, e->crtc_id)) {
926 *crtc_id = e->crtc_id;
932 /* Try to find a possible crtc which will work */
933 for (i = 0; i < r->count_crtcs; i++) {
934 if (!(e->possible_crtcs & (1 << i)))
937 /* We've already tried this earlier */
938 if (e->crtc_id == r->crtcs[i])
941 if (!hwc_crtc_is_bound(ctx, r->crtcs[i])) {
942 *crtc_id = r->crtcs[i];
948 /* We can't use the encoder, but nothing went wrong, try another one */
952 drmModeFreeEncoder(e);
956 static int hwc_set_active_config(struct hwc_composer_device_1* dev, int display,
959 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
960 struct hwc_drm_display *hd = NULL;
961 drmModeResPtr r = NULL;
962 drmModeConnectorPtr c;
963 uint32_t crtc_id = 0;
965 bool new_crtc, new_encoder;
967 ret = hwc_get_drm_display(ctx, display, &hd);
971 c = drmModeGetConnector(ctx->fd, hd->connector_id);
973 ALOGE("Failed to get connector %d", display);
977 if (c->connection == DRM_MODE_DISCONNECTED) {
978 ALOGE("Tried to configure a disconnected display %d", display);
983 if (index >= c->count_modes) {
984 ALOGE("Index is out-of-bounds %d/%d", index, c->count_modes);
989 r = drmModeGetResources(ctx->fd);
991 ALOGE("Failed to get drm resources");
995 /* We no longer have an active_crtc */
997 hd->active_pipe = -1;
999 /* First, try to use the currently-connected encoder */
1000 if (c->encoder_id) {
1001 ret = hwc_try_encoder(ctx, r, c->encoder_id, &crtc_id);
1002 if (ret && ret != -EAGAIN) {
1003 ALOGE("Encoder try failed %d", ret);
1008 /* We couldn't find a crtc with the attached encoder, try the others */
1010 for (i = 0; i < c->count_encoders; i++) {
1011 ret = hwc_try_encoder(ctx, r, c->encoders[i], &crtc_id);
1014 } else if (ret != -EAGAIN) {
1015 ALOGE("Encoder try failed %d", ret);
1020 ALOGE("Couldn't find valid crtc to modeset");
1026 hd->active_crtc = crtc_id;
1028 memcpy(&hd->active_mode, &hd->configs[index], sizeof(hd->active_mode));
1030 /* Find the pipe corresponding to the crtc_id */
1031 for (i = 0; i < r->count_crtcs; i++) {
1032 /* We've already tried this earlier */
1033 if (r->crtcs[i] == crtc_id) {
1034 hd->active_pipe = i;
1038 /* This should never happen... hehehe */
1039 if (hd->active_pipe == -1) {
1040 ALOGE("Active crtc was not found in resources!!");
1045 /* TODO: Once we have atomic, set the crtc timing info here */
1049 drmModeFreeResources(r);
1051 drmModeFreeConnector(c);
1055 static int hwc_destroy_worker(struct hwc_worker *worker)
1059 ret = pthread_mutex_lock(&worker->lock);
1061 ALOGE("Failed to lock in destroy() %d", ret);
1065 worker->exit = true;
1067 ret |= pthread_cond_signal(&worker->cond);
1069 ALOGE("Failed to signal cond in destroy() %d", ret);
1071 ret |= pthread_mutex_unlock(&worker->lock);
1073 ALOGE("Failed to unlock in destroy() %d", ret);
1075 ret |= pthread_join(worker->thread, NULL);
1076 if (ret && ret != ESRCH)
1077 ALOGE("Failed to join thread in destroy() %d", ret);
1082 static void hwc_destroy_display(struct hwc_drm_display *hd)
1086 if (hwc_destroy_worker(&hd->set_worker))
1087 ALOGE("Destroy set worker failed");
1089 if (hwc_destroy_worker(&hd->vsync_worker))
1090 ALOGE("Destroy vsync worker failed");
1093 static int hwc_device_close(struct hw_device_t *dev)
1095 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
1098 for (i = 0; i < MAX_NUM_DISPLAYS; i++)
1099 hwc_destroy_display(&ctx->displays[i]);
1103 ret = hwc_import_destroy(ctx->import_ctx);
1105 ALOGE("Could not destroy import %d", ret);
1112 static int hwc_initialize_worker(struct hwc_drm_display *hd,
1113 struct hwc_worker *worker, void *(*routine)(void*))
1117 ret = pthread_cond_init(&worker->cond, NULL);
1119 ALOGE("Failed to create worker condition %d", ret);
1123 ret = pthread_mutex_init(&worker->lock, NULL);
1125 ALOGE("Failed to initialize worker lock %d", ret);
1129 worker->exit = false;
1131 ret = pthread_create(&worker->thread, NULL, routine, hd);
1133 ALOGE("Could not create worker thread %d", ret);
1139 pthread_mutex_destroy(&worker->lock);
1141 pthread_cond_destroy(&worker->cond);
1146 * TODO: This function sets the active config to the first one in the list. This
1147 * should be fixed such that it selects the preferred mode for the display, or
1148 * some other, saner, method of choosing the config.
1150 static int hwc_set_initial_config(struct hwc_drm_display *hd)
1154 size_t num_configs = 1;
1156 ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
1158 if (ret || !num_configs)
1161 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
1163 ALOGE("Failed to set active config d=%d ret=%d", hd->display,
1171 static int hwc_initialize_display(struct hwc_context_t *ctx, int display,
1172 uint32_t connector_id)
1174 struct hwc_drm_display *hd = NULL;
1177 ret = hwc_get_drm_display(ctx, display, &hd);
1182 hd->display = display;
1183 hd->active_pipe = -1;
1184 hd->initial_modeset_required = true;
1185 hd->connector_id = connector_id;
1187 ret = sw_sync_timeline_create();
1189 ALOGE("Failed to create sw sync timeline %d", ret);
1192 hd->timeline_fd = ret;
1194 ret = hwc_set_initial_config(hd);
1196 ALOGE("Failed to set initial config for d=%d ret=%d", display,
1201 ret = hwc_initialize_worker(hd, &hd->set_worker, hwc_set_worker);
1203 ALOGE("Failed to create set worker %d\n", ret);
1207 ret = hwc_initialize_worker(hd, &hd->vsync_worker, hwc_vsync_worker);
1209 ALOGE("Failed to create vsync worker %d", ret);
1216 if (hwc_destroy_worker(&hd->set_worker))
1217 ALOGE("Failed to destroy set worker");
1222 static int hwc_enumerate_displays(struct hwc_context_t *ctx)
1224 struct hwc_drm_display *panel_hd;
1226 drmModeConnectorPtr *conn_list;
1229 res = drmModeGetResources(ctx->fd);
1231 ALOGE("Failed to get drm resources");
1235 conn_list = (drmModeConnector **)calloc(res->count_connectors,
1236 sizeof(*conn_list));
1238 ALOGE("Failed to allocate connector list");
1243 for (i = 0; i < res->count_connectors; i++) {
1244 conn_list[i] = drmModeGetConnector(ctx->fd, res->connectors[i]);
1245 if (!conn_list[i]) {
1246 ALOGE("Failed to get connector %d", res->connectors[i]);
1252 ctx->num_displays = 0;
1254 /* Find a connected, panel type connector for display 0 */
1255 for (i = 0; i < res->count_connectors; i++) {
1256 drmModeConnectorPtr c = conn_list[i];
1258 for (j = 0; j < ARRAY_SIZE(panel_types); j++) {
1259 if (c->connector_type == panel_types[j] &&
1260 c->connection == DRM_MODE_CONNECTED)
1263 if (j == ARRAY_SIZE(panel_types))
1266 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1267 ctx->num_displays++;
1271 ret = hwc_get_drm_display(ctx, 0, &panel_hd);
1275 /* Fill in the other displays */
1276 for (i = 0; i < res->count_connectors; i++) {
1277 drmModeConnectorPtr c = conn_list[i];
1279 if (panel_hd->connector_id == c->connector_id)
1282 hwc_initialize_display(ctx, ctx->num_displays, c->connector_id);
1283 ctx->num_displays++;
1287 for (i = 0; i < res->count_connectors; i++) {
1289 drmModeFreeConnector(conn_list[i]);
1294 drmModeFreeResources(res);
1299 static int hwc_device_open(const struct hw_module_t* module, const char* name,
1300 struct hw_device_t** dev)
1303 struct hwc_context_t *ctx;
1304 char path[PROPERTY_VALUE_MAX];
1306 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1307 ALOGE("Invalid module name- %s", name);
1311 ctx = new hwc_context_t();
1313 ALOGE("Failed to allocate hwc context");
1317 ret = hwc_import_init(&ctx->import_ctx);
1319 ALOGE("Failed to initialize import context");
1323 property_get("hwc.drm.device", path, HWCOMPOSER_DRM_DEVICE);
1324 /* TODO: Use drmOpenControl here instead */
1325 ctx->fd = open(path, O_RDWR);
1327 ALOGE("Failed to open dri- %s", strerror(-errno));
1331 ret = hwc_enumerate_displays(ctx);
1333 ALOGE("Failed to enumerate displays: %s", strerror(ret));
1337 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1338 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
1339 ctx->device.common.module = const_cast<hw_module_t*>(module);
1340 ctx->device.common.close = hwc_device_close;
1342 ctx->device.prepare = hwc_prepare;
1343 ctx->device.set = hwc_set;
1344 ctx->device.eventControl = hwc_event_control;
1345 ctx->device.setPowerMode = hwc_set_power_mode;
1346 ctx->device.query = hwc_query;
1347 ctx->device.registerProcs = hwc_register_procs;
1348 ctx->device.getDisplayConfigs = hwc_get_display_configs;
1349 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
1350 ctx->device.getActiveConfig = hwc_get_active_config;
1351 ctx->device.setActiveConfig = hwc_set_active_config;
1352 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
1354 *dev = &ctx->device.common;
1365 static struct hw_module_methods_t hwc_module_methods = {
1366 open: hwc_device_open
1369 hwc_module_t HAL_MODULE_INFO_SYM = {
1371 tag: HARDWARE_MODULE_TAG,
1374 id: HWC_HARDWARE_MODULE_ID,
1375 name: "DRM hwcomposer module",
1376 author: "The Android Open Source Project",
1377 methods: &hwc_module_methods,