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>
33 #include "gralloc_drm.h"
34 #include "gralloc_drm_priv.h"
36 #include <drm_fourcc.h>
39 * Return true if a bo needs fb.
41 int gralloc_drm_bo_need_fb(const struct gralloc_drm_bo_t *bo)
43 return ((bo->handle->usage & GRALLOC_USAGE_HW_FB) &&
44 bo->drm->swap_mode != DRM_SWAP_COPY);
48 * Modify pitches, offsets and handles according to
49 * the format and return corresponding drm format value
51 static int resolve_drm_format(struct gralloc_drm_bo_t *bo,
52 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
54 memset(pitches, 0, 4 * sizeof(uint32_t));
55 memset(offsets, 0, 4 * sizeof(uint32_t));
56 memset(handles, 0, 4 * sizeof(uint32_t));
58 pitches[0] = bo->handle->stride;
59 handles[0] = bo->fb_handle;
61 switch(bo->handle->format) {
62 case HAL_PIXEL_FORMAT_RGB_888:
63 case HAL_PIXEL_FORMAT_RGBX_8888:
64 case HAL_PIXEL_FORMAT_BGRA_8888:
65 return DRM_FORMAT_ARGB8888;
66 case HAL_PIXEL_FORMAT_RGBA_8888:
67 return DRM_FORMAT_RGBA8888;
68 case HAL_PIXEL_FORMAT_RGB_565:
69 return DRM_FORMAT_RGB565;
70 case HAL_PIXEL_FORMAT_YV12:
72 // U and V stride are half of Y plane
73 pitches[2] = pitches[0]/2;
74 pitches[1] = pitches[0]/2;
76 // like I420 but U and V are in reverse order
77 offsets[2] = offsets[0] +
78 pitches[0] * bo->handle->height;
79 offsets[1] = offsets[2] +
80 pitches[2] * bo->handle->height/2;
82 handles[1] = handles[2] = handles[0];
84 return DRM_FORMAT_YUV420;
92 * Add a fb object for a bo.
94 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo)
103 int drm_format = resolve_drm_format(bo, pitches, offsets, handles);
105 if (drm_format == 0) {
106 ALOGE("error resolving drm format");
110 return drmModeAddFB2(bo->drm->fd,
111 bo->handle->width, bo->handle->height,
112 drm_format, handles, pitches, offsets,
113 (uint32_t *) &bo->fb_id, 0);
117 * Remove a fb object for a bo.
119 void gralloc_drm_bo_rm_fb(struct gralloc_drm_bo_t *bo)
122 drmModeRmFB(bo->drm->fd, bo->fb_id);
130 static int drm_kms_set_crtc(struct gralloc_drm_t *drm, int fb_id)
134 ret = drmModeSetCrtc(drm->fd, drm->crtc_id, fb_id,
135 0, 0, &drm->connector_id, 1, &drm->mode);
137 ALOGE("failed to set crtc");
141 if (drm->mode_quirk_vmwgfx)
142 ret = drmModeDirtyFB(drm->fd, fb_id, &drm->clip, 1);
148 * Callback for a page flip event.
150 static void page_flip_handler(int fd, unsigned int sequence,
151 unsigned int tv_sec, unsigned int tv_usec,
154 struct gralloc_drm_t *drm = (struct gralloc_drm_t *) user_data;
156 /* ack the last scheduled flip */
157 drm->current_front = drm->next_front;
158 drm->next_front = NULL;
162 * Schedule a page flip.
164 static int drm_kms_page_flip(struct gralloc_drm_t *drm,
165 struct gralloc_drm_bo_t *bo)
169 /* there is another flip pending */
170 while (drm->next_front) {
171 drm->waiting_flip = 1;
172 drmHandleEvent(drm->fd, &drm->evctx);
173 drm->waiting_flip = 0;
174 if (drm->next_front) {
175 /* record an error and break */
176 ALOGE("drmHandleEvent returned without flipping");
177 drm->current_front = drm->next_front;
178 drm->next_front = NULL;
185 ret = drmModePageFlip(drm->fd, drm->crtc_id, bo->fb_id,
186 DRM_MODE_PAGE_FLIP_EVENT, (void *) drm);
188 ALOGE("failed to perform page flip");
190 drm->next_front = bo;
196 * Wait for the next post.
198 static void drm_kms_wait_for_post(struct gralloc_drm_t *drm, int flip)
200 unsigned int current, target;
204 if (drm->mode_quirk_vmwgfx)
209 memset(&vbl, 0, sizeof(vbl));
210 vbl.request.type = DRM_VBLANK_RELATIVE;
211 if (drm->vblank_secondary)
212 vbl.request.type |= DRM_VBLANK_SECONDARY;
213 vbl.request.sequence = 0;
215 /* get the current vblank */
216 ret = drmWaitVBlank(drm->fd, &vbl);
218 ALOGW("failed to get vblank");
222 current = vbl.reply.sequence;
226 target = drm->last_swap + drm->swap_interval - flip;
228 /* wait for vblank */
229 if (current < target || !flip) {
230 memset(&vbl, 0, sizeof(vbl));
231 vbl.request.type = DRM_VBLANK_ABSOLUTE;
232 if (drm->vblank_secondary)
233 vbl.request.type |= DRM_VBLANK_SECONDARY;
235 vbl.request.type |= DRM_VBLANK_NEXTONMISS;
236 if (target < current)
240 vbl.request.sequence = target;
242 ret = drmWaitVBlank(drm->fd, &vbl);
244 ALOGW("failed to wait vblank");
249 drm->last_swap = vbl.reply.sequence + flip;
253 * Post a bo. This is not thread-safe.
255 int gralloc_drm_bo_post(struct gralloc_drm_bo_t *bo)
257 struct gralloc_drm_t *drm = bo->drm;
260 if (!bo->fb_id && drm->swap_mode != DRM_SWAP_COPY) {
261 ALOGE("unable to post bo %p without fb", bo);
265 /* TODO spawn a thread to avoid waiting and race */
267 if (drm->first_post) {
268 if (drm->swap_mode == DRM_SWAP_COPY) {
269 struct gralloc_drm_bo_t *dst;
271 dst = (drm->next_front) ?
274 drm->drv->copy(drm->drv, dst, bo, 0, 0,
280 ret = drm_kms_set_crtc(drm, bo->fb_id);
283 drm->current_front = bo;
284 if (drm->next_front == bo)
285 drm->next_front = NULL;
291 switch (drm->swap_mode) {
293 if (drm->swap_interval > 1)
294 drm_kms_wait_for_post(drm, 1);
295 ret = drm_kms_page_flip(drm, bo);
296 if (drm->next_front) {
298 * wait if the driver says so or the current front
299 * will be written by CPU
301 if (drm->mode_sync_flip ||
302 (drm->current_front->handle->usage &
303 GRALLOC_USAGE_SW_WRITE_MASK))
304 drm_kms_page_flip(drm, NULL);
308 drm_kms_wait_for_post(drm, 0);
309 drm->drv->copy(drm->drv, drm->current_front,
313 if (drm->mode_quirk_vmwgfx)
314 ret = drmModeDirtyFB(drm->fd, drm->current_front->fb_id, &drm->clip, 1);
317 case DRM_SWAP_SETCRTC:
318 drm_kms_wait_for_post(drm, 0);
319 ret = drm_kms_set_crtc(drm, bo->fb_id);
320 drm->current_front = bo;
331 static struct gralloc_drm_t *drm_singleton;
333 static void on_signal(int sig)
335 struct gralloc_drm_t *drm = drm_singleton;
337 /* wait the pending flip */
338 if (drm && drm->swap_mode == DRM_SWAP_FLIP && drm->next_front) {
339 /* there is race, but this function is hacky enough to ignore that */
340 if (drm_singleton->waiting_flip)
341 usleep(100 * 1000); /* 100ms */
343 drm_kms_page_flip(drm_singleton, NULL);
349 static void drm_kms_init_features(struct gralloc_drm_t *drm)
351 const char *swap_mode;
353 /* call to the driver here, after KMS has been initialized */
354 drm->drv->init_kms_features(drm->drv, drm);
356 if (drm->swap_mode == DRM_SWAP_FLIP) {
357 struct sigaction act;
359 memset(&drm->evctx, 0, sizeof(drm->evctx));
360 drm->evctx.version = DRM_EVENT_CONTEXT_VERSION;
361 drm->evctx.page_flip_handler = page_flip_handler;
364 * XXX GPU tends to freeze if the program is terminiated with a
365 * flip pending. What is the right way to handle the
368 sigemptyset(&act.sa_mask);
369 act.sa_handler = on_signal;
371 sigaction(SIGINT, &act, NULL);
372 sigaction(SIGTERM, &act, NULL);
376 else if (drm->swap_mode == DRM_SWAP_COPY) {
377 struct gralloc_drm_bo_t *front;
380 /* create the real front buffer */
381 front = gralloc_drm_bo_create(drm,
385 GRALLOC_USAGE_HW_FB);
386 if (front && gralloc_drm_bo_add_fb(front)) {
387 gralloc_drm_bo_decref(front);
391 /* abuse next_front */
393 drm->next_front = front;
395 drm->swap_mode = DRM_SWAP_SETCRTC;
398 switch (drm->swap_mode) {
405 case DRM_SWAP_SETCRTC:
406 swap_mode = "set-crtc";
413 ALOGD("will use %s for fb posting", swap_mode);
416 static drmModeModeInfoPtr find_mode(drmModeConnectorPtr connector, int *bpp)
418 char value[PROPERTY_VALUE_MAX];
419 drmModeModeInfoPtr mode;
421 int xres = 0, yres = 0;
423 if (property_get("debug.drm.mode", value, NULL)) {
424 char *p = value, *end;
426 /* parse <xres>x<yres>[@<bpp>] */
427 if (sscanf(value, "%dx%d@%d", &xres, &yres, bpp) != 3) {
429 if (sscanf(value, "%dx%d", &xres, &yres) != 2)
433 if ((xres && yres) || *bpp) {
434 ALOGI("will find the closest match for %dx%d@%d",
444 for (i = 0; i < connector->count_modes; i++) {
445 drmModeModeInfoPtr m = &connector->modes[i];
449 tmp = (m->hdisplay - xres) * (m->hdisplay - xres) +
450 (m->vdisplay - yres) * (m->vdisplay - yres);
453 /* use the first preferred mode */
454 tmp = (m->type & DRM_MODE_TYPE_PREFERRED) ? 0 : dist;
465 /* fallback to the first mode */
467 mode = &connector->modes[0];
475 * Initialize KMS with a connector.
477 static int drm_kms_init_with_connector(struct gralloc_drm_t *drm,
478 drmModeConnectorPtr connector)
480 drmModeEncoderPtr encoder;
481 drmModeModeInfoPtr mode;
484 if (!connector->count_modes)
487 encoder = drmModeGetEncoder(drm->fd, connector->encoders[0]);
491 for (i = 0; i < drm->resources->count_crtcs; i++) {
492 if (encoder->possible_crtcs & (1 << i))
495 drmModeFreeEncoder(encoder);
496 if (i == drm->resources->count_crtcs)
499 drm->crtc_id = drm->resources->crtcs[i];
500 drm->connector_id = connector->connector_id;
502 /* print connector info */
503 if (connector->count_modes > 1) {
504 ALOGI("there are %d modes on connector 0x%x",
505 connector->count_modes,
506 connector->connector_id);
507 for (i = 0; i < connector->count_modes; i++)
508 ALOGI(" %s", connector->modes[i].name);
511 ALOGI("there is one mode on connector 0x%d: %s",
512 connector->connector_id,
513 connector->modes[0].name);
516 mode = find_mode(connector, &bpp);
518 ALOGI("the best mode is %s", mode->name);
523 drm->fb_format = HAL_PIXEL_FORMAT_RGB_565;
527 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
531 if (connector->mmWidth && connector->mmHeight) {
532 drm->xdpi = (drm->mode.hdisplay * 25.4 / connector->mmWidth);
533 drm->ydpi = (drm->mode.vdisplay * 25.4 / connector->mmHeight);
540 #ifdef DRM_MODE_FEATURE_DIRTYFB
543 drm->clip.x2 = drm->mode.hdisplay;
544 drm->clip.y2 = drm->mode.vdisplay;
553 int gralloc_drm_init_kms(struct gralloc_drm_t *drm)
560 drm->resources = drmModeGetResources(drm->fd);
561 if (!drm->resources) {
562 ALOGE("failed to get modeset resources");
566 /* find the crtc/connector/mode to use */
567 for (i = 0; i < drm->resources->count_connectors; i++) {
568 drmModeConnectorPtr connector;
570 connector = drmModeGetConnector(drm->fd,
571 drm->resources->connectors[i]);
573 if (connector->connection == DRM_MODE_CONNECTED) {
574 if (!drm_kms_init_with_connector(drm,
579 drmModeFreeConnector(connector);
582 if (i == drm->resources->count_connectors) {
583 ALOGE("failed to find a valid crtc/connector/mode combination");
584 drmModeFreeResources(drm->resources);
585 drm->resources = NULL;
590 drm_kms_init_features(drm);
596 void gralloc_drm_fini_kms(struct gralloc_drm_t *drm)
598 switch (drm->swap_mode) {
600 drm_kms_page_flip(drm, NULL);
604 struct gralloc_drm_bo_t **bo = (drm->current_front) ?
605 &drm->current_front : &drm->next_front;
608 gralloc_drm_bo_decref(*bo);
618 if (drm->resources) {
619 drmModeFreeResources(drm->resources);
620 drm->resources = NULL;
623 drm_singleton = NULL;
626 int gralloc_drm_is_kms_initialized(struct gralloc_drm_t *drm)
628 return (drm->resources != NULL);
632 * Initialize a framebuffer device with KMS info.
634 void gralloc_drm_get_kms_info(struct gralloc_drm_t *drm,
635 struct framebuffer_device_t *fb)
637 *((uint32_t *) &fb->flags) = 0x0;
638 *((uint32_t *) &fb->width) = drm->mode.hdisplay;
639 *((uint32_t *) &fb->height) = drm->mode.vdisplay;
640 *((int *) &fb->stride) = drm->mode.hdisplay;
641 *((float *) &fb->fps) = drm->mode.vrefresh;
643 *((int *) &fb->format) = drm->fb_format;
644 *((float *) &fb->xdpi) = drm->xdpi;
645 *((float *) &fb->ydpi) = drm->ydpi;
646 *((int *) &fb->minSwapInterval) = drm->swap_interval;
647 *((int *) &fb->maxSwapInterval) = drm->swap_interval;
651 * Return true if fb posting is pipelined.
653 int gralloc_drm_is_kms_pipelined(struct gralloc_drm_t *drm)
655 return (drm->swap_mode != DRM_SWAP_SETCRTC);