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;
68 * Modify pitches, offsets and handles according to
69 * the format and return corresponding drm format value
71 static int resolve_drm_format(struct gralloc_drm_bo_t *bo,
72 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
74 memset(pitches, 0, 4 * sizeof(uint32_t));
75 memset(offsets, 0, 4 * sizeof(uint32_t));
76 memset(handles, 0, 4 * sizeof(uint32_t));
78 pitches[0] = bo->handle->stride;
79 handles[0] = bo->fb_handle;
81 int format = drm_format_from_hal(bo->handle->format);
83 // handle 'special formats'
84 switch(bo->handle->format) {
85 case HAL_PIXEL_FORMAT_YV12:
87 // U and V stride are half of Y plane
88 pitches[2] = pitches[0]/2;
89 pitches[1] = pitches[0]/2;
91 // like I420 but U and V are in reverse order
92 offsets[2] = offsets[0] +
93 pitches[0] * bo->handle->height;
94 offsets[1] = offsets[2] +
95 pitches[2] * bo->handle->height/2;
97 handles[1] = handles[2] = handles[0];
103 * Add a fb object for a bo.
105 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo)
114 int drm_format = resolve_drm_format(bo, pitches, offsets, handles);
116 if (drm_format == 0) {
117 ALOGE("error resolving drm format");
121 return drmModeAddFB2(bo->drm->fd,
122 bo->handle->width, bo->handle->height,
123 drm_format, handles, pitches, offsets,
124 (uint32_t *) &bo->fb_id, 0);
128 * Remove a fb object for a bo.
130 void gralloc_drm_bo_rm_fb(struct gralloc_drm_bo_t *bo)
133 drmModeRmFB(bo->drm->fd, bo->fb_id);
141 static int drm_kms_set_crtc(struct gralloc_drm_t *drm,
142 struct gralloc_drm_output *output, int fb_id)
146 ret = drmModeSetCrtc(drm->fd, output->crtc_id, fb_id,
147 0, 0, &output->connector_id, 1, &output->mode);
149 ALOGE("failed to set crtc (%s) (crtc_id %d, fb_id %d, conn %d, mode %dx%d)",
150 strerror(errno), output->crtc_id, fb_id, output->connector_id,
151 output->mode.hdisplay, output->mode.vdisplay);
155 if (drm->mode_quirk_vmwgfx)
156 ret = drmModeDirtyFB(drm->fd, fb_id, &drm->clip, 1);
162 * Callback for a page flip event.
164 static void page_flip_handler(int fd, unsigned int sequence,
165 unsigned int tv_sec, unsigned int tv_usec,
168 struct gralloc_drm_t *drm = (struct gralloc_drm_t *) user_data;
170 /* ack the last scheduled flip */
171 drm->current_front = drm->next_front;
172 drm->next_front = NULL;
176 * Schedule a page flip.
178 static int drm_kms_page_flip(struct gralloc_drm_t *drm,
179 struct gralloc_drm_bo_t *bo)
183 /* there is another flip pending */
184 while (drm->next_front) {
185 drm->waiting_flip = 1;
186 drmHandleEvent(drm->fd, &drm->evctx);
187 drm->waiting_flip = 0;
188 if (drm->next_front) {
189 /* record an error and break */
190 ALOGE("drmHandleEvent returned without flipping");
191 drm->current_front = drm->next_front;
192 drm->next_front = NULL;
199 pthread_mutex_lock(&drm->hdmi_mutex);
200 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED) {
201 ret = drmModePageFlip(drm->fd, drm->hdmi.crtc_id, bo->fb_id, 0, NULL);
202 if (ret && errno != EBUSY)
203 ALOGE("failed to perform page flip for hdmi (%s) (crtc %d fb %d))",
204 strerror(errno), drm->hdmi.crtc_id, bo->fb_id);
206 pthread_mutex_unlock(&drm->hdmi_mutex);
208 ret = drmModePageFlip(drm->fd, drm->primary.crtc_id, bo->fb_id,
209 DRM_MODE_PAGE_FLIP_EVENT, (void *) drm);
211 ALOGE("failed to perform page flip for primary (%s) (crtc %d fb %d))",
212 strerror(errno), drm->primary.crtc_id, bo->fb_id);
213 /* try to set mode for next frame */
217 drm->next_front = bo;
223 * Wait for the next post.
225 static void drm_kms_wait_for_post(struct gralloc_drm_t *drm, int flip)
227 unsigned int current, target;
231 if (drm->mode_quirk_vmwgfx)
236 memset(&vbl, 0, sizeof(vbl));
237 vbl.request.type = DRM_VBLANK_RELATIVE;
238 if (drm->vblank_secondary)
239 vbl.request.type |= DRM_VBLANK_SECONDARY;
240 vbl.request.sequence = 0;
242 /* get the current vblank */
243 ret = drmWaitVBlank(drm->fd, &vbl);
245 ALOGW("failed to get vblank");
249 current = vbl.reply.sequence;
253 target = drm->last_swap + drm->swap_interval - flip;
255 /* wait for vblank */
256 if (current < target || !flip) {
257 memset(&vbl, 0, sizeof(vbl));
258 vbl.request.type = DRM_VBLANK_ABSOLUTE;
259 if (drm->vblank_secondary)
260 vbl.request.type |= DRM_VBLANK_SECONDARY;
262 vbl.request.type |= DRM_VBLANK_NEXTONMISS;
263 if (target < current)
267 vbl.request.sequence = target;
269 ret = drmWaitVBlank(drm->fd, &vbl);
271 ALOGW("failed to wait vblank");
276 drm->last_swap = vbl.reply.sequence + flip;
280 * Post a bo. This is not thread-safe.
282 int gralloc_drm_bo_post(struct gralloc_drm_bo_t *bo)
284 struct gralloc_drm_t *drm = bo->drm;
287 if (!bo->fb_id && drm->swap_mode != DRM_SWAP_COPY) {
288 ALOGE("unable to post bo %p without fb", bo);
292 /* TODO spawn a thread to avoid waiting and race */
294 if (drm->first_post) {
295 if (drm->swap_mode == DRM_SWAP_COPY) {
296 struct gralloc_drm_bo_t *dst;
298 dst = (drm->next_front) ?
301 drm->drv->copy(drm->drv, dst, bo, 0, 0,
307 ret = drm_kms_set_crtc(drm, &drm->primary, bo->fb_id);
310 drm->current_front = bo;
311 if (drm->next_front == bo)
312 drm->next_front = NULL;
315 pthread_mutex_lock(&drm->hdmi_mutex);
316 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED)
317 drm_kms_set_crtc(drm, &drm->hdmi, bo->fb_id);
318 pthread_mutex_unlock(&drm->hdmi_mutex);
323 switch (drm->swap_mode) {
325 if (drm->swap_interval > 1)
326 drm_kms_wait_for_post(drm, 1);
327 ret = drm_kms_page_flip(drm, bo);
328 if (drm->next_front) {
330 * wait if the driver says so or the current front
331 * will be written by CPU
333 if (drm->mode_sync_flip ||
334 (drm->current_front->handle->usage &
335 GRALLOC_USAGE_SW_WRITE_MASK))
336 drm_kms_page_flip(drm, NULL);
340 drm_kms_wait_for_post(drm, 0);
341 drm->drv->copy(drm->drv, drm->current_front,
345 if (drm->mode_quirk_vmwgfx)
346 ret = drmModeDirtyFB(drm->fd, drm->current_front->fb_id, &drm->clip, 1);
349 case DRM_SWAP_SETCRTC:
350 drm_kms_wait_for_post(drm, 0);
351 ret = drm_kms_set_crtc(drm, &drm->primary, bo->fb_id);
353 pthread_mutex_lock(&drm->hdmi_mutex);
354 if (drm->hdmi.active && drm->hdmi_mode == HDMI_CLONED)
355 drm_kms_set_crtc(drm, &drm->hdmi, bo->fb_id);
356 pthread_mutex_unlock(&drm->hdmi_mutex);
358 drm->current_front = bo;
369 static struct gralloc_drm_t *drm_singleton;
371 static void on_signal(int sig)
373 struct gralloc_drm_t *drm = drm_singleton;
375 /* wait the pending flip */
376 if (drm && drm->swap_mode == DRM_SWAP_FLIP && drm->next_front) {
377 /* there is race, but this function is hacky enough to ignore that */
378 if (drm_singleton->waiting_flip)
379 usleep(100 * 1000); /* 100ms */
381 drm_kms_page_flip(drm_singleton, NULL);
387 static void drm_kms_init_features(struct gralloc_drm_t *drm)
389 const char *swap_mode;
391 /* call to the driver here, after KMS has been initialized */
392 drm->drv->init_kms_features(drm->drv, drm);
394 if (drm->swap_mode == DRM_SWAP_FLIP) {
395 struct sigaction act;
397 memset(&drm->evctx, 0, sizeof(drm->evctx));
398 drm->evctx.version = DRM_EVENT_CONTEXT_VERSION;
399 drm->evctx.page_flip_handler = page_flip_handler;
402 * XXX GPU tends to freeze if the program is terminiated with a
403 * flip pending. What is the right way to handle the
406 sigemptyset(&act.sa_mask);
407 act.sa_handler = on_signal;
409 sigaction(SIGINT, &act, NULL);
410 sigaction(SIGTERM, &act, NULL);
414 else if (drm->swap_mode == DRM_SWAP_COPY) {
415 struct gralloc_drm_bo_t *front;
418 /* create the real front buffer */
419 front = gralloc_drm_bo_create(drm,
420 drm->primary.mode.hdisplay,
421 drm->primary.mode.vdisplay,
422 drm->primary.fb_format,
423 GRALLOC_USAGE_HW_FB);
424 if (front && gralloc_drm_bo_add_fb(front)) {
425 gralloc_drm_bo_decref(front);
429 /* abuse next_front */
431 drm->next_front = front;
433 drm->swap_mode = DRM_SWAP_SETCRTC;
436 switch (drm->swap_mode) {
443 case DRM_SWAP_SETCRTC:
444 swap_mode = "set-crtc";
451 ALOGD("will use %s for fb posting", swap_mode);
454 static drmModeModeInfoPtr find_mode(drmModeConnectorPtr connector, int *bpp)
456 char value[PROPERTY_VALUE_MAX];
457 drmModeModeInfoPtr mode;
459 int xres = 0, yres = 0;
461 if (property_get("debug.drm.mode", value, NULL)) {
462 char *p = value, *end;
464 /* parse <xres>x<yres>[@<bpp>] */
465 if (sscanf(value, "%dx%d@%d", &xres, &yres, bpp) != 3) {
467 if (sscanf(value, "%dx%d", &xres, &yres) != 2)
471 if ((xres && yres) || *bpp) {
472 ALOGI("will find the closest match for %dx%d@%d",
482 for (i = 0; i < connector->count_modes; i++) {
483 drmModeModeInfoPtr m = &connector->modes[i];
487 tmp = (m->hdisplay - xres) * (m->hdisplay - xres) +
488 (m->vdisplay - yres) * (m->vdisplay - yres);
491 /* use the first preferred mode */
492 tmp = (m->type & DRM_MODE_TYPE_PREFERRED) ? 0 : dist;
503 /* fallback to the first mode */
505 mode = &connector->modes[0];
513 * Initialize KMS with a connector.
515 static int drm_kms_init_with_connector(struct gralloc_drm_t *drm,
516 struct gralloc_drm_output *output, drmModeConnectorPtr connector)
518 drmModeEncoderPtr encoder;
519 drmModeModeInfoPtr mode;
522 if (!connector->count_modes)
525 encoder = drmModeGetEncoder(drm->fd, connector->encoders[0]);
529 /* find first possible crtc which is not used yet */
530 for (i = 0; i < drm->resources->count_crtcs; i++) {
531 if (encoder->possible_crtcs & (1 << i) &&
532 i != drm->primary.crtc_id)
535 drmModeFreeEncoder(encoder);
536 if (i == drm->resources->count_crtcs)
539 output->crtc_id = drm->resources->crtcs[i];
540 output->connector_id = connector->connector_id;
542 /* print connector info */
543 if (connector->count_modes > 1) {
544 ALOGI("there are %d modes on connector 0x%x",
545 connector->count_modes,
546 connector->connector_id);
547 for (i = 0; i < connector->count_modes; i++)
548 ALOGI(" %s", connector->modes[i].name);
551 ALOGI("there is one mode on connector 0x%d: %s",
552 connector->connector_id,
553 connector->modes[0].name);
556 mode = find_mode(connector, &bpp);
558 ALOGI("the best mode is %s", mode->name);
560 output->mode = *mode;
563 output->fb_format = HAL_PIXEL_FORMAT_RGB_565;
567 output->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
571 if (connector->mmWidth && connector->mmHeight) {
572 output->xdpi = (output->mode.hdisplay * 25.4 / connector->mmWidth);
573 output->ydpi = (output->mode.vdisplay * 25.4 / connector->mmHeight);
580 #ifdef DRM_MODE_FEATURE_DIRTYFB
583 drm->clip.x2 = output->mode.hdisplay;
584 drm->clip.y2 = output->mode.vdisplay;
592 * Fetch a connector of particular type
594 static drmModeConnectorPtr fetch_connector(struct gralloc_drm_t *drm,
602 for (i = 0; i < drm->resources->count_connectors; i++) {
603 drmModeConnectorPtr connector =
604 connector = drmModeGetConnector(drm->fd,
605 drm->resources->connectors[i]);
607 if (connector->connector_type == type &&
608 connector->connection == DRM_MODE_CONNECTED)
610 drmModeFreeConnector(connector);
618 * Thread that listens to uevents and checks if hdmi state changes
620 static void *hdmi_observer(void *data)
622 static char uevent_desc[4096];
623 drmModeConnectorPtr hdmi;
624 struct gralloc_drm_t *drm =
625 (struct gralloc_drm_t *) data;
629 memset(uevent_desc, 0, sizeof(uevent_desc));
634 int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
636 if(len && strstr(uevent_desc, "devices/virtual/switch/hdmi")) {
638 /* check what changed */
639 const char *prop = uevent_desc + strlen(uevent_desc) + 1;
643 const char *state = strstr(prop, "SWITCH_STATE=");
645 unsigned int value = 0;
646 state += strlen("SWITCH_STATE=");
649 pthread_mutex_lock(&drm->hdmi_mutex);
652 hdmi = fetch_connector(drm, DRM_MODE_CONNECTOR_HDMIA);
654 drm_kms_init_with_connector(drm, &drm->hdmi, hdmi);
655 drmModeFreeConnector(hdmi);
657 /* will trigger modeset */
660 /* HACK, assume same mode for now */
661 memcpy(&drm->hdmi.mode, &drm->primary.mode,
662 sizeof(drmModeModeInfo));
664 drm->hdmi_mode = HDMI_CLONED;
665 drm->hdmi.active = 1;
666 pthread_mutex_unlock(&drm->hdmi_mutex);
670 drm->hdmi.active = 0;
671 pthread_mutex_unlock(&drm->hdmi_mutex);
675 pthread_mutex_unlock(&drm->hdmi_mutex);
678 /* next property/value pair */
679 prop += strlen(prop) + 1;
680 if (prop - uevent_desc >= len)
694 int gralloc_drm_init_kms(struct gralloc_drm_t *drm)
696 drmModeConnectorPtr lvds, hdmi;
702 drm->resources = drmModeGetResources(drm->fd);
703 if (!drm->resources) {
704 ALOGE("failed to get modeset resources");
708 drm->plane_resources = drmModeGetPlaneResources(drm->fd);
709 if (!drm->plane_resources) {
710 ALOGD("no planes found from drm resources");
712 ALOGD("supported drm planes and formats");
713 /* fill a helper structure for hwcomposer */
714 drm->planes = calloc(drm->plane_resources->count_planes,
715 sizeof(struct gralloc_drm_plane_t));
717 for (i = 0; i < drm->plane_resources->count_planes; i++) {
721 drm->planes[i].drm_plane = drmModeGetPlane(drm->fd,
722 drm->plane_resources->planes[i]);
724 ALOGD("plane id %d", drm->planes[i].drm_plane->plane_id);
725 for (j = 0; j < drm->planes[i].drm_plane->count_formats; j++)
726 ALOGD(" format %c%c%c%c",
727 (drm->planes[i].drm_plane->formats[j]),
728 (drm->planes[i].drm_plane->formats[j])>>8,
729 (drm->planes[i].drm_plane->formats[j])>>16,
730 (drm->planes[i].drm_plane->formats[j])>>24);
734 /* find the crtc/connector/mode to use */
735 lvds = fetch_connector(drm, DRM_MODE_CONNECTOR_LVDS);
737 drm_kms_init_with_connector(drm, &drm->primary, lvds);
738 drmModeFreeConnector(lvds);
739 drm->primary.active = 1;
742 /* if still no connector, find first connected connector and try it */
743 if (!drm->primary.active) {
745 for (i = 0; i < drm->resources->count_connectors; i++) {
746 drmModeConnectorPtr connector;
748 connector = drmModeGetConnector(drm->fd,
749 drm->resources->connectors[i]);
751 if (connector->connection == DRM_MODE_CONNECTED) {
752 if (!drm_kms_init_with_connector(drm,
753 &drm->primary, connector))
757 drmModeFreeConnector(connector);
760 if (i == drm->resources->count_connectors) {
761 ALOGE("failed to find a valid crtc/connector/mode combination");
762 drmModeFreeResources(drm->resources);
763 drm->resources = NULL;
769 /* check if hdmi is connected already */
770 hdmi = fetch_connector(drm, DRM_MODE_CONNECTOR_HDMIA);
772 drm_kms_init_with_connector(drm, &drm->hdmi, hdmi);
773 drmModeFreeConnector(hdmi);
775 /* HACK, assume same mode for now */
776 memcpy(&drm->hdmi.mode, &drm->primary.mode,
777 sizeof(drmModeModeInfo));
779 drm->hdmi_mode = HDMI_CLONED;
780 drm->hdmi.active = 1;
783 /* launch hdmi observer thread */
784 pthread_mutex_init(&drm->hdmi_mutex, NULL);
785 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);