/* * Copyright © 2011 Kristian Høgsberg * Copyright © 2011 Benjamin Franzke * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that copyright * notice and this permission notice appear in supporting documentation, and * that the name of the copyright holders not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. The copyright holders make no representations * about the suitability of this software for any purpose. It is provided "as * is" without express or implied warranty. * * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. */ #include #include #define EGL_EGLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES #include #include #include #include #include #include #include #include struct kms { drmModeConnector *connector; drmModeEncoder *encoder; drmModeModeInfo mode; uint32_t fb_id; }; static bool setup_kms(int fd, struct kms *kms) { drmModeRes *resources; drmModeConnector *connector; drmModeEncoder *encoder; int i; int count; resources = drmModeGetResources(fd); if (!resources) { fprintf(stderr, "drmModeGetResources failed\n"); return false; } for (i = 0; i < resources->count_connectors; i++) { connector = drmModeGetConnector(fd, resources->connectors[i]); if (connector == NULL) continue; if (connector->connection == DRM_MODE_CONNECTED && connector->count_modes > 0 && count++ > 0) break; drmModeFreeConnector(connector); } if (i == resources->count_connectors) { fprintf(stderr, "No currently active connector found.\n"); return false; } for (i = 0; i < resources->count_encoders; i++) { encoder = drmModeGetEncoder(fd, resources->encoders[i]); if (encoder == NULL) continue; if (encoder->encoder_id == connector->encoder_id) break; drmModeFreeEncoder(encoder); } kms->connector = connector; kms->encoder = encoder; kms->mode = connector->modes[0]; return true; } static const char device_name[] = "/dev/dri/card0"; int main(int argc, char *argv[]) { uint32_t handle; long unsigned int stride; struct kms kms; int ret, fd; drm_intel_bufmgr *bufmgr; drm_intel_bo *bo, *untiled_bo; uint32_t tiling; uint32_t *pixels; int x, y; drmModeCrtcPtr saved_crtc; fd = open(device_name, O_RDWR); if (fd < 0) { /* Probably permissions error */ fprintf(stderr, "couldn't open %s, skipping\n", device_name); return -1; } bufmgr = drm_intel_bufmgr_gem_init(fd, 4096); if (bufmgr == NULL) { fprintf(stderr, "couldn't create gbm device\n"); ret = -1; goto close_fd; } if (!setup_kms(fd, &kms)) { ret = -1; goto egl_terminate; } tiling = I915_TILING_X; bo = drm_intel_bo_alloc_tiled(bufmgr, "test", kms.mode.hdisplay, kms.mode.vdisplay, 4, &tiling, &stride, 0); handle = bo->handle; printf("handle=%d, stride=%d\n", handle, stride); ret = drmModeAddFB(fd, kms.mode.hdisplay, kms.mode.vdisplay, 24, 32, stride, handle, &kms.fb_id); if (ret) { fprintf(stderr, "failed to create fb\n"); goto rm_fb; } saved_crtc = drmModeGetCrtc(fd, kms.encoder->crtc_id); if (saved_crtc == NULL) goto rm_fb; ret = drmModeSetCrtc(fd, kms.encoder->crtc_id, kms.fb_id, 0, 0, &kms.connector->connector_id, 1, &kms.mode); if (ret) { fprintf(stderr, "failed to set mode: %m\n"); goto free_saved_crtc; } tiling = I915_TILING_NONE; untiled_bo = drm_intel_bo_alloc_tiled(bufmgr, "test", kms.mode.hdisplay, kms.mode.vdisplay, 4, &tiling, &stride, 0); handle = untiled_bo->handle; printf("handle=%d, stride=%d\n", handle, stride); drm_intel_gem_bo_map_gtt(untiled_bo); pixels = untiled_bo->virtual; for (y = 0; y < kms.mode.vdisplay; y++) { for (x = 0; x < kms.mode.hdisplay; x++) if (y % 2 == 0) pixels[x] = 0xff | ((int) ((float) y / kms.mode.vdisplay * 255)) << 8; else pixels[x] = 0; pixels += stride / 4; } drm_intel_bo_unmap(untiled_bo); ret = drmModeAddFB(fd, kms.mode.hdisplay, kms.mode.vdisplay, 24, 32, stride, handle, &kms.fb_id); if (ret) { fprintf(stderr, "failed to create second fb\n"); goto rm_fb; } ret = drmModePageFlip(fd, kms.encoder->crtc_id, kms.fb_id, 0, NULL); if (ret) { fprintf(stderr, "page flip failed: %m\n"); goto rm_fb; } getchar(); ret = drmModeSetCrtc(fd, saved_crtc->crtc_id, saved_crtc->buffer_id, saved_crtc->x, saved_crtc->y, &kms.connector->connector_id, 1, &saved_crtc->mode); if (ret) { fprintf(stderr, "failed to restore crtc: %m\n"); } free_saved_crtc: drmModeFreeCrtc(saved_crtc); rm_fb: drmModeRmFB(fd, kms.fb_id); egl_terminate: close_fd: close(fd); return ret; }