2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #define LOG_TAG "GRALLOC-KMS"
26 #include <cutils/properties.h>
27 #include <cutils/log.h>
34 #include "gralloc_drm.h"
35 #include "gralloc_drm_priv.h"
36 #include <hardware_legacy/uevent.h>
38 #include <drm_fourcc.h>
41 * Return true if a bo needs fb.
43 int gralloc_drm_bo_need_fb(const struct gralloc_drm_bo_t *bo)
45 return ((bo->handle->usage & GRALLOC_USAGE_HW_FB) &&
46 bo->drm->swap_mode != DRM_SWAP_COPY);
49 static unsigned int drm_format_from_hal(int hal_format)
52 case HAL_PIXEL_FORMAT_RGB_888:
53 case HAL_PIXEL_FORMAT_RGBX_8888:
54 case HAL_PIXEL_FORMAT_BGRA_8888:
55 return DRM_FORMAT_XRGB8888;
56 case HAL_PIXEL_FORMAT_RGBA_8888:
57 return DRM_FORMAT_RGBA8888;
58 case HAL_PIXEL_FORMAT_RGB_565:
59 return DRM_FORMAT_RGB565;
60 case HAL_PIXEL_FORMAT_YV12:
61 return DRM_FORMAT_YUV420;
62 case HAL_PIXEL_FORMAT_DRM_NV12:
63 return DRM_FORMAT_NV12;
70 * Modify pitches, offsets and handles according to
71 * the format and return corresponding drm format value
73 static int resolve_drm_format(struct gralloc_drm_bo_t *bo,
74 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
76 struct gralloc_drm_t *drm = bo->drm;
78 pitches[0] = bo->handle->stride;
79 handles[0] = bo->fb_handle;
81 /* driver takes care of HW specific padding, alignment etc. */
82 if (drm->drv->resolve_format)
83 drm->drv->resolve_format(drm->drv, bo,
84 pitches, offsets, handles);
86 return drm_format_from_hal(bo->handle->format);
90 * Add a fb object for a bo.
92 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo)
94 uint32_t pitches[4] = { 0, 0, 0, 0 };
95 uint32_t offsets[4] = { 0, 0, 0, 0 };
96 uint32_t handles[4] = { 0, 0, 0, 0 };
101 int drm_format = resolve_drm_format(bo, pitches, offsets, handles);
103 if (drm_format == 0) {
104 ALOGE("error resolving drm format");
108 return drmModeAddFB2(bo->drm->fd,
109 bo->handle->width, bo->handle->height,
110 drm_format, handles, pitches, offsets,
111 (uint32_t *) &bo->fb_id, 0);
115 * Remove a fb object for a bo.
117 void gralloc_drm_bo_rm_fb(struct gralloc_drm_bo_t *bo)
120 drmModeRmFB(bo->drm->fd, bo->fb_id);
128 static int drm_kms_set_crtc(struct gralloc_drm_t *drm,
129 struct gralloc_drm_output *output, int fb_id)
133 ret = drmModeSetCrtc(drm->fd, output->crtc_id, fb_id,
134 0, 0, &output->connector_id, 1, &output->mode);
136 ALOGE("failed to set crtc (%s) (crtc_id %d, fb_id %d, conn %d, mode %dx%d)",
137 strerror(errno), output->crtc_id, fb_id, output->connector_id,
138 output->mode.hdisplay, output->mode.vdisplay);
142 if (drm->mode_quirk_vmwgfx)
143 ret = drmModeDirtyFB(drm->fd, fb_id, &drm->clip, 1);
149 * Callback for a page flip event.
151 static void page_flip_handler(int fd, unsigned int sequence,
152 unsigned int tv_sec, unsigned int tv_usec,
155 struct gralloc_drm_t *drm = (struct gralloc_drm_t *) user_data;
157 /* ack the last scheduled flip */
158 drm->current_front = drm->next_front;
159 drm->next_front = NULL;
163 * Schedule a page flip.
165 static int drm_kms_page_flip(struct gralloc_drm_t *drm,
166 struct gralloc_drm_bo_t *bo)
170 /* there is another flip pending */
171 while (drm->next_front) {
172 drm->waiting_flip = 1;
173 drmHandleEvent(drm->fd, &drm->evctx);
174 drm->waiting_flip = 0;
175 if (drm->next_front) {
176 /* record an error and break */
177 ALOGE("drmHandleEvent returned without flipping");
178 drm->current_front = drm->next_front;
179 drm->next_front = NULL;
186 pthread_mutex_lock(&drm->hdmi_mutex);
187 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED) {
188 ret = drmModePageFlip(drm->fd, drm->hdmi.crtc_id, bo->fb_id, 0, NULL);
189 if (ret && errno != EBUSY)
190 ALOGE("failed to perform page flip for hdmi (%s) (crtc %d fb %d))",
191 strerror(errno), drm->hdmi.crtc_id, bo->fb_id);
193 pthread_mutex_unlock(&drm->hdmi_mutex);
195 ret = drmModePageFlip(drm->fd, drm->primary.crtc_id, bo->fb_id,
196 DRM_MODE_PAGE_FLIP_EVENT, (void *) drm);
198 ALOGE("failed to perform page flip for primary (%s) (crtc %d fb %d))",
199 strerror(errno), drm->primary.crtc_id, bo->fb_id);
200 /* try to set mode for next frame */
205 drm->next_front = bo;
211 * Wait for the next post.
213 static void drm_kms_wait_for_post(struct gralloc_drm_t *drm, int flip)
215 unsigned int current, target;
219 if (drm->mode_quirk_vmwgfx)
224 memset(&vbl, 0, sizeof(vbl));
225 vbl.request.type = DRM_VBLANK_RELATIVE;
226 if (drm->vblank_secondary)
227 vbl.request.type |= DRM_VBLANK_SECONDARY;
228 vbl.request.sequence = 0;
230 /* get the current vblank */
231 ret = drmWaitVBlank(drm->fd, &vbl);
233 ALOGW("failed to get vblank");
237 current = vbl.reply.sequence;
241 target = drm->last_swap + drm->swap_interval - flip;
243 /* wait for vblank */
244 if (current < target || !flip) {
245 memset(&vbl, 0, sizeof(vbl));
246 vbl.request.type = DRM_VBLANK_ABSOLUTE;
247 if (drm->vblank_secondary)
248 vbl.request.type |= DRM_VBLANK_SECONDARY;
250 vbl.request.type |= DRM_VBLANK_NEXTONMISS;
251 if (target < current)
255 vbl.request.sequence = target;
257 ret = drmWaitVBlank(drm->fd, &vbl);
259 ALOGW("failed to wait vblank");
264 drm->last_swap = vbl.reply.sequence + flip;
268 * Post a bo. This is not thread-safe.
270 int gralloc_drm_bo_post(struct gralloc_drm_bo_t *bo)
272 struct gralloc_drm_t *drm = bo->drm;
275 if (!bo->fb_id && drm->swap_mode != DRM_SWAP_COPY) {
276 ALOGE("unable to post bo %p without fb", bo);
280 /* TODO spawn a thread to avoid waiting and race */
282 if (drm->first_post) {
283 if (drm->swap_mode == DRM_SWAP_COPY) {
284 struct gralloc_drm_bo_t *dst;
286 dst = (drm->next_front) ?
289 drm->drv->copy(drm->drv, dst, bo, 0, 0,
295 ret = drm_kms_set_crtc(drm, &drm->primary, bo->fb_id);
298 drm->current_front = bo;
299 if (drm->next_front == bo)
300 drm->next_front = NULL;
303 pthread_mutex_lock(&drm->hdmi_mutex);
304 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED)
305 drm_kms_set_crtc(drm, &drm->hdmi, bo->fb_id);
306 pthread_mutex_unlock(&drm->hdmi_mutex);
311 switch (drm->swap_mode) {
313 if (drm->swap_interval > 1)
314 drm_kms_wait_for_post(drm, 1);
315 ret = drm_kms_page_flip(drm, bo);
316 if (drm->next_front) {
318 * wait if the driver says so or the current front
319 * will be written by CPU
321 if (drm->mode_sync_flip ||
322 (drm->current_front->handle->usage &
323 GRALLOC_USAGE_SW_WRITE_MASK))
324 drm_kms_page_flip(drm, NULL);
328 drm_kms_wait_for_post(drm, 0);
329 drm->drv->copy(drm->drv, drm->current_front,
333 if (drm->mode_quirk_vmwgfx)
334 ret = drmModeDirtyFB(drm->fd, drm->current_front->fb_id, &drm->clip, 1);
337 case DRM_SWAP_SETCRTC:
338 drm_kms_wait_for_post(drm, 0);
339 ret = drm_kms_set_crtc(drm, &drm->primary, bo->fb_id);
341 pthread_mutex_lock(&drm->hdmi_mutex);
342 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED)
343 drm_kms_set_crtc(drm, &drm->hdmi, bo->fb_id);
344 pthread_mutex_unlock(&drm->hdmi_mutex);
346 drm->current_front = bo;
357 static struct gralloc_drm_t *drm_singleton;
359 static void on_signal(int sig)
361 struct gralloc_drm_t *drm = drm_singleton;
363 /* wait the pending flip */
364 if (drm && drm->swap_mode == DRM_SWAP_FLIP && drm->next_front) {
365 /* there is race, but this function is hacky enough to ignore that */
366 if (drm_singleton->waiting_flip)
367 usleep(100 * 1000); /* 100ms */
369 drm_kms_page_flip(drm_singleton, NULL);
375 static void drm_kms_init_features(struct gralloc_drm_t *drm)
377 const char *swap_mode;
379 /* call to the driver here, after KMS has been initialized */
380 drm->drv->init_kms_features(drm->drv, drm);
382 if (drm->swap_mode == DRM_SWAP_FLIP) {
383 struct sigaction act;
385 memset(&drm->evctx, 0, sizeof(drm->evctx));
386 drm->evctx.version = DRM_EVENT_CONTEXT_VERSION;
387 drm->evctx.page_flip_handler = page_flip_handler;
390 * XXX GPU tends to freeze if the program is terminiated with a
391 * flip pending. What is the right way to handle the
394 sigemptyset(&act.sa_mask);
395 act.sa_handler = on_signal;
397 sigaction(SIGINT, &act, NULL);
398 sigaction(SIGTERM, &act, NULL);
402 else if (drm->swap_mode == DRM_SWAP_COPY) {
403 struct gralloc_drm_bo_t *front;
406 /* create the real front buffer */
407 front = gralloc_drm_bo_create(drm,
408 drm->primary.mode.hdisplay,
409 drm->primary.mode.vdisplay,
410 drm->primary.fb_format,
411 GRALLOC_USAGE_HW_FB);
412 if (front && gralloc_drm_bo_add_fb(front)) {
413 gralloc_drm_bo_decref(front);
417 /* abuse next_front */
419 drm->next_front = front;
421 drm->swap_mode = DRM_SWAP_SETCRTC;
424 switch (drm->swap_mode) {
431 case DRM_SWAP_SETCRTC:
432 swap_mode = "set-crtc";
439 ALOGD("will use %s for fb posting", swap_mode);
442 static drmModeModeInfoPtr find_mode(drmModeConnectorPtr connector, int *bpp)
444 char value[PROPERTY_VALUE_MAX];
445 drmModeModeInfoPtr mode;
447 int xres = 0, yres = 0;
449 if (property_get("debug.drm.mode", value, NULL)) {
450 char *p = value, *end;
452 /* parse <xres>x<yres>[@<bpp>] */
453 if (sscanf(value, "%dx%d@%d", &xres, &yres, bpp) != 3) {
455 if (sscanf(value, "%dx%d", &xres, &yres) != 2)
459 if ((xres && yres) || *bpp) {
460 ALOGI("will find the closest match for %dx%d@%d",
470 for (i = 0; i < connector->count_modes; i++) {
471 drmModeModeInfoPtr m = &connector->modes[i];
475 tmp = (m->hdisplay - xres) * (m->hdisplay - xres) +
476 (m->vdisplay - yres) * (m->vdisplay - yres);
479 /* use the first preferred mode */
480 tmp = (m->type & DRM_MODE_TYPE_PREFERRED) ? 0 : dist;
491 /* fallback to the first mode */
493 mode = &connector->modes[0];
501 * Initialize KMS with a connector.
503 static int drm_kms_init_with_connector(struct gralloc_drm_t *drm,
504 struct gralloc_drm_output *output, drmModeConnectorPtr connector)
506 drmModeEncoderPtr encoder;
507 drmModeModeInfoPtr mode;
508 static int used_crtcs = 0;
511 if (!connector->count_modes)
514 encoder = drmModeGetEncoder(drm->fd, connector->encoders[0]);
518 /* find first possible crtc which is not used yet */
519 for (i = 0; i < drm->resources->count_crtcs; i++) {
520 if (encoder->possible_crtcs & (1 << i) &&
521 (used_crtcs & (1 << i)) != (1 << i))
525 used_crtcs |= (1 << i);
527 drmModeFreeEncoder(encoder);
528 if (i == drm->resources->count_crtcs)
531 output->crtc_id = drm->resources->crtcs[i];
532 output->connector_id = connector->connector_id;
534 /* print connector info */
535 if (connector->count_modes > 1) {
536 ALOGI("there are %d modes on connector 0x%x",
537 connector->count_modes,
538 connector->connector_id);
539 for (i = 0; i < connector->count_modes; i++)
540 ALOGI(" %s", connector->modes[i].name);
543 ALOGI("there is one mode on connector 0x%d: %s",
544 connector->connector_id,
545 connector->modes[0].name);
548 mode = find_mode(connector, &bpp);
550 ALOGI("the best mode is %s", mode->name);
552 output->mode = *mode;
555 output->fb_format = HAL_PIXEL_FORMAT_RGB_565;
559 output->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
563 if (connector->mmWidth && connector->mmHeight) {
564 output->xdpi = (output->mode.hdisplay * 25.4 / connector->mmWidth);
565 output->ydpi = (output->mode.vdisplay * 25.4 / connector->mmHeight);
572 #ifdef DRM_MODE_FEATURE_DIRTYFB
575 drm->clip.x2 = output->mode.hdisplay;
576 drm->clip.y2 = output->mode.vdisplay;
584 * Fetch a connector of particular type
586 static drmModeConnectorPtr fetch_connector(struct gralloc_drm_t *drm,
594 for (i = 0; i < drm->resources->count_connectors; i++) {
595 drmModeConnectorPtr connector =
596 connector = drmModeGetConnector(drm->fd,
597 drm->resources->connectors[i]);
599 if (connector->connector_type == type &&
600 connector->connection == DRM_MODE_CONNECTED)
602 drmModeFreeConnector(connector);
610 * Thread that listens to uevents and checks if hdmi state changes
612 static void *hdmi_observer(void *data)
614 static char uevent_desc[4096];
615 drmModeConnectorPtr hdmi;
616 struct gralloc_drm_t *drm =
617 (struct gralloc_drm_t *) data;
621 memset(uevent_desc, 0, sizeof(uevent_desc));
626 int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
628 if(len && strstr(uevent_desc, "devices/virtual/switch/hdmi")) {
630 /* check what changed */
631 const char *prop = uevent_desc + strlen(uevent_desc) + 1;
635 const char *state = strstr(prop, "SWITCH_STATE=");
637 unsigned int value = 0;
638 state += strlen("SWITCH_STATE=");
641 pthread_mutex_lock(&drm->hdmi_mutex);
644 hdmi = fetch_connector(drm, DRM_MODE_CONNECTOR_HDMIA);
646 drm_kms_init_with_connector(drm, &drm->hdmi, hdmi);
647 drmModeFreeConnector(hdmi);
649 /* will trigger modeset */
652 /* HACK, assume same mode for now */
653 memcpy(&drm->hdmi.mode, &drm->primary.mode,
654 sizeof(drmModeModeInfo));
656 drm->hdmi_mode = HDMI_CLONED;
657 drm->hdmi.active = 1;
658 pthread_mutex_unlock(&drm->hdmi_mutex);
662 drm->hdmi.active = 0;
663 pthread_mutex_unlock(&drm->hdmi_mutex);
667 pthread_mutex_unlock(&drm->hdmi_mutex);
670 /* next property/value pair */
671 prop += strlen(prop) + 1;
672 if (prop - uevent_desc >= len)
686 int gralloc_drm_init_kms(struct gralloc_drm_t *drm)
688 drmModeConnectorPtr lvds, hdmi;
694 drm->resources = drmModeGetResources(drm->fd);
695 if (!drm->resources) {
696 ALOGE("failed to get modeset resources");
700 drm->plane_resources = drmModeGetPlaneResources(drm->fd);
701 if (!drm->plane_resources) {
702 ALOGD("no planes found from drm resources");
704 ALOGD("supported drm planes and formats");
705 /* fill a helper structure for hwcomposer */
706 drm->planes = calloc(drm->plane_resources->count_planes,
707 sizeof(struct gralloc_drm_plane_t));
709 for (i = 0; i < drm->plane_resources->count_planes; i++) {
713 drm->planes[i].drm_plane = drmModeGetPlane(drm->fd,
714 drm->plane_resources->planes[i]);
716 ALOGD("plane id %d", drm->planes[i].drm_plane->plane_id);
717 for (j = 0; j < drm->planes[i].drm_plane->count_formats; j++)
718 ALOGD(" format %c%c%c%c",
719 (drm->planes[i].drm_plane->formats[j]),
720 (drm->planes[i].drm_plane->formats[j])>>8,
721 (drm->planes[i].drm_plane->formats[j])>>16,
722 (drm->planes[i].drm_plane->formats[j])>>24);
726 /* find the crtc/connector/mode to use */
727 lvds = fetch_connector(drm, DRM_MODE_CONNECTOR_LVDS);
729 drm_kms_init_with_connector(drm, &drm->primary, lvds);
730 drmModeFreeConnector(lvds);
731 drm->primary.active = 1;
734 /* if still no connector, find first connected connector and try it */
735 if (!drm->primary.active) {
737 for (i = 0; i < drm->resources->count_connectors; i++) {
738 drmModeConnectorPtr connector;
740 connector = drmModeGetConnector(drm->fd,
741 drm->resources->connectors[i]);
743 if (connector->connection == DRM_MODE_CONNECTED) {
744 if (!drm_kms_init_with_connector(drm,
745 &drm->primary, connector))
749 drmModeFreeConnector(connector);
752 if (i == drm->resources->count_connectors) {
753 ALOGE("failed to find a valid crtc/connector/mode combination");
754 drmModeFreeResources(drm->resources);
755 drm->resources = NULL;
761 /* check if hdmi is connected already */
762 hdmi = fetch_connector(drm, DRM_MODE_CONNECTOR_HDMIA);
765 if (hdmi->connector_id == drm->primary.connector_id) {
766 /* special case: our primary connector is hdmi */
767 goto skip_hdmi_modes;
770 drm_kms_init_with_connector(drm, &drm->hdmi, hdmi);
771 drmModeFreeConnector(hdmi);
773 /* HACK, assume same mode for now */
774 memcpy(&drm->hdmi.mode, &drm->primary.mode,
775 sizeof(drmModeModeInfo));
777 drm->hdmi_mode = HDMI_CLONED;
778 drm->hdmi.active = 1;
781 /* launch hdmi observer thread */
782 pthread_mutex_init(&drm->hdmi_mutex, NULL);
783 pthread_create(&drm->hdmi_hotplug_thread, NULL, hdmi_observer, drm);
787 drm_kms_init_features(drm);
793 void gralloc_drm_fini_kms(struct gralloc_drm_t *drm)
795 switch (drm->swap_mode) {
797 drm_kms_page_flip(drm, NULL);
801 struct gralloc_drm_bo_t **bo = (drm->current_front) ?
802 &drm->current_front : &drm->next_front;
805 gralloc_drm_bo_decref(*bo);
815 if (drm->resources) {
816 drmModeFreeResources(drm->resources);
817 drm->resources = NULL;
822 for (i = 0; i < drm->plane_resources->count_planes; i++)
823 drmModeFreePlane(drm->planes[i].drm_plane);
828 if (drm->plane_resources) {
829 drmModeFreePlaneResources(drm->plane_resources);
830 drm->plane_resources = NULL;
833 drm_singleton = NULL;
836 int gralloc_drm_is_kms_initialized(struct gralloc_drm_t *drm)
838 return (drm->resources != NULL);
842 * Initialize a framebuffer device with KMS info.
844 void gralloc_drm_get_kms_info(struct gralloc_drm_t *drm,
845 struct framebuffer_device_t *fb)
847 *((uint32_t *) &fb->flags) = 0x0;
848 *((uint32_t *) &fb->width) = drm->primary.mode.hdisplay;
849 *((uint32_t *) &fb->height) = drm->primary.mode.vdisplay;
850 *((int *) &fb->stride) = drm->primary.mode.hdisplay;
851 *((float *) &fb->fps) = drm->primary.mode.vrefresh;
853 *((int *) &fb->format) = drm->primary.fb_format;
854 *((float *) &fb->xdpi) = drm->primary.xdpi;
855 *((float *) &fb->ydpi) = drm->primary.ydpi;
856 *((int *) &fb->minSwapInterval) = drm->swap_interval;
857 *((int *) &fb->maxSwapInterval) = drm->swap_interval;
861 * Return true if fb posting is pipelined.
863 int gralloc_drm_is_kms_pipelined(struct gralloc_drm_t *drm)
865 return (drm->swap_mode != DRM_SWAP_SETCRTC);