Back to index

libdrm  2.4.37
dumb.c
Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
00021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
00022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
00023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
00024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00028 
00029 #define HAVE_STDINT_H
00030 #define _FILE_OFFSET_BITS 64
00031 
00032 #include <errno.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include "internal.h"
00037 
00038 #include <sys/mman.h>
00039 #include <sys/ioctl.h>
00040 #include "xf86drm.h"
00041 
00042 #include "i915_drm.h"
00043 
00044 struct dumb_bo
00045 {
00046        struct kms_bo base;
00047        unsigned map_count;
00048 };
00049 
00050 static int
00051 dumb_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
00052 {
00053        switch (key) {
00054        case KMS_BO_TYPE:
00055               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
00056               break;
00057        default:
00058               return -EINVAL;
00059        }
00060        return 0;
00061 }
00062 
00063 static int
00064 dumb_destroy(struct kms_driver *kms)
00065 {
00066        free(kms);
00067        return 0;
00068 }
00069 
00070 static int
00071 dumb_bo_create(struct kms_driver *kms,
00072                const unsigned width, const unsigned height,
00073                const enum kms_bo_type type, const unsigned *attr,
00074                struct kms_bo **out)
00075 {
00076        struct drm_mode_create_dumb arg;
00077        struct dumb_bo *bo;
00078        int i, ret;
00079 
00080        for (i = 0; attr[i]; i += 2) {
00081               switch (attr[i]) {
00082               case KMS_WIDTH:
00083               case KMS_HEIGHT:
00084                      break;
00085               case KMS_BO_TYPE:
00086                      break;
00087               default:
00088                      return -EINVAL;
00089               }
00090        }
00091 
00092        bo = calloc(1, sizeof(*bo));
00093        if (!bo)
00094               return -ENOMEM;
00095 
00096        memset(&arg, 0, sizeof(arg));
00097 
00098        /* All BO_TYPE currently are 32bpp formats */
00099        arg.bpp = 32;
00100        arg.width = width;
00101        arg.height = height;
00102 
00103        ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
00104        if (ret)
00105               goto err_free;
00106 
00107        bo->base.kms = kms;
00108        bo->base.handle = arg.handle;
00109        bo->base.size = arg.size;
00110        bo->base.pitch = arg.pitch;
00111 
00112        *out = &bo->base;
00113 
00114        return 0;
00115 
00116 err_free:
00117        free(bo);
00118        return ret;
00119 }
00120 
00121 static int
00122 dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
00123 {
00124        switch (key) {
00125        default:
00126               return -EINVAL;
00127        }
00128 }
00129 
00130 static int
00131 dumb_bo_map(struct kms_bo *_bo, void **out)
00132 {
00133        struct dumb_bo *bo = (struct dumb_bo *)_bo;
00134        struct drm_mode_map_dumb arg;
00135        void *map = NULL;
00136        int ret;
00137 
00138        if (bo->base.ptr) {
00139               bo->map_count++;
00140               *out = bo->base.ptr;
00141               return 0;
00142        }
00143 
00144        memset(&arg, 0, sizeof(arg));
00145        arg.handle = bo->base.handle;
00146 
00147        ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
00148        if (ret)
00149               return ret;
00150 
00151        map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
00152        if (map == MAP_FAILED)
00153               return -errno;
00154 
00155        bo->base.ptr = map;
00156        bo->map_count++;
00157        *out = bo->base.ptr;
00158 
00159        return 0;
00160 }
00161 
00162 static int
00163 dumb_bo_unmap(struct kms_bo *_bo)
00164 {
00165        struct dumb_bo *bo = (struct dumb_bo *)_bo;
00166        bo->map_count--;
00167        return 0;
00168 }
00169 
00170 static int
00171 dumb_bo_destroy(struct kms_bo *_bo)
00172 {
00173        struct dumb_bo *bo = (struct dumb_bo *)_bo;
00174        struct drm_mode_destroy_dumb arg;
00175        int ret;
00176 
00177        if (bo->base.ptr) {
00178               /* XXX Sanity check map_count */
00179               munmap(bo->base.ptr, bo->base.size);
00180               bo->base.ptr = NULL;
00181        }
00182 
00183        memset(&arg, 0, sizeof(arg));
00184        arg.handle = bo->base.handle;
00185 
00186        ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
00187        if (ret)
00188               return -errno;
00189 
00190        free(bo);
00191        return 0;
00192 }
00193 
00194 int
00195 dumb_create(int fd, struct kms_driver **out)
00196 {
00197        struct kms_driver *kms;
00198        int ret;
00199        uint64_t cap = 0;
00200 
00201        ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
00202        if (ret || cap == 0)
00203               return -EINVAL;
00204 
00205        kms = calloc(1, sizeof(*kms));
00206        if (!kms)
00207               return -ENOMEM;
00208 
00209        kms->fd = fd;
00210 
00211        kms->bo_create = dumb_bo_create;
00212        kms->bo_map = dumb_bo_map;
00213        kms->bo_unmap = dumb_bo_unmap;
00214        kms->bo_get_prop = dumb_bo_get_prop;
00215        kms->bo_destroy = dumb_bo_destroy;
00216        kms->get_prop = dumb_get_prop;
00217        kms->destroy = dumb_destroy;
00218        *out = kms;
00219 
00220        return 0;
00221 }