xserver

xserver with xephyr scale patch
git clone https://git.neptards.moe/u3shit/xserver.git
Log | Files | Refs | README | LICENSE

dumb_bo.c (3252B)


      1 /*
      2  * Copyright © 2007 Red Hat, Inc.
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     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 FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  *
     23  * Authors:
     24  *    Dave Airlie <airlied@redhat.com>
     25  *
     26  */
     27 
     28 #ifdef HAVE_DIX_CONFIG_H
     29 #include "dix-config.h"
     30 #endif
     31 
     32 #include "dumb_bo.h"
     33 
     34 #include <errno.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/mman.h>
     39 #include <unistd.h>
     40 #include <xf86drm.h>
     41 
     42 struct dumb_bo *
     43 dumb_bo_create(int fd,
     44                const unsigned width, const unsigned height, const unsigned bpp)
     45 {
     46     struct drm_mode_create_dumb arg;
     47     struct dumb_bo *bo;
     48     int ret;
     49 
     50     bo = calloc(1, sizeof(*bo));
     51     if (!bo)
     52         return NULL;
     53 
     54     memset(&arg, 0, sizeof(arg));
     55     arg.width = width;
     56     arg.height = height;
     57     arg.bpp = bpp;
     58 
     59     ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
     60     if (ret)
     61         goto err_free;
     62 
     63     bo->handle = arg.handle;
     64     bo->size = arg.size;
     65     bo->pitch = arg.pitch;
     66 
     67     return bo;
     68  err_free:
     69     free(bo);
     70     return NULL;
     71 }
     72 
     73 int
     74 dumb_bo_map(int fd, struct dumb_bo *bo)
     75 {
     76     struct drm_mode_map_dumb arg;
     77     int ret;
     78     void *map;
     79 
     80     if (bo->ptr) {
     81         return 0;
     82     }
     83 
     84     memset(&arg, 0, sizeof(arg));
     85     arg.handle = bo->handle;
     86 
     87     ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
     88     if (ret)
     89         return ret;
     90 
     91     map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, arg.offset);
     92     if (map == MAP_FAILED)
     93         return -errno;
     94 
     95     bo->ptr = map;
     96     return 0;
     97 }
     98 
     99 int
    100 dumb_bo_destroy(int fd, struct dumb_bo *bo)
    101 {
    102     struct drm_mode_destroy_dumb arg;
    103     int ret;
    104 
    105     if (bo->ptr) {
    106         munmap(bo->ptr, bo->size);
    107         bo->ptr = NULL;
    108     }
    109 
    110     memset(&arg, 0, sizeof(arg));
    111     arg.handle = bo->handle;
    112     ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
    113     if (ret)
    114         return -errno;
    115 
    116     free(bo);
    117     return 0;
    118 }
    119 
    120 struct dumb_bo *
    121 dumb_get_bo_from_fd(int fd, int handle, int pitch, int size)
    122 {
    123     struct dumb_bo *bo;
    124     int ret;
    125 
    126     bo = calloc(1, sizeof(*bo));
    127     if (!bo)
    128         return NULL;
    129 
    130     ret = drmPrimeFDToHandle(fd, handle, &bo->handle);
    131     if (ret) {
    132         free(bo);
    133         return NULL;
    134     }
    135     bo->pitch = pitch;
    136     bo->size = size;
    137     return bo;
    138 }