OSDN Git Service

minigbm: Remove drv prefix from static backend functions
[android-x86/external-minigbm.git] / rockchip.c
1 /*
2  * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #ifdef DRV_ROCKCHIP
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <xf86drm.h>
15 #include <rockchip_drm.h>
16
17 #include "drv_priv.h"
18 #include "helpers.h"
19 #include "util.h"
20
21 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height,
22                               uint32_t format, uint32_t flags)
23 {
24         size_t plane;
25
26         switch (format) {
27                 case DRV_FORMAT_NV12:
28                         width = ALIGN(width, 4);
29                         height = ALIGN(height, 4);
30                         bo->strides[0] = bo->strides[1] = width;
31                         bo->sizes[0] = height * bo->strides[0];
32                         bo->sizes[1] = height * bo->strides[1] / 2;
33                         bo->offsets[0] = 0;
34                         bo->offsets[1] = height * bo->strides[0];
35                         break;
36                 case DRV_FORMAT_XRGB8888:
37                 case DRV_FORMAT_ARGB8888:
38                 case DRV_FORMAT_ABGR8888:
39                         bo->strides[0] = drv_stride_from_format(format, width);
40                         bo->sizes[0] = height * bo->strides[0];
41                         bo->offsets[0] = 0;
42                         break;
43                 default:
44                         fprintf(stderr, "drv: rockchip: unsupported format %4.4s\n",
45                                 (char*)&format);
46                         assert(0);
47                         return -EINVAL;
48         }
49
50         int ret;
51         size_t size = 0;
52
53         for (plane = 0; plane < bo->num_planes; plane++)
54                 size += bo->sizes[plane];
55
56         struct drm_rockchip_gem_create gem_create;
57
58         memset(&gem_create, 0, sizeof(gem_create));
59         gem_create.size = size;
60
61         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE,
62                            &gem_create);
63
64         if (ret) {
65                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed "
66                                 "(size=%zu)\n", size);
67         }
68         else {
69                 for (plane = 0; plane < bo->num_planes; plane++)
70                         bo->handles[plane].u32 = gem_create.handle;
71         }
72
73         return ret;
74 }
75
76 static void *rockchip_bo_map(struct bo *bo)
77 {
78         int ret;
79         struct drm_rockchip_gem_map_off gem_map;
80
81         memset(&gem_map, 0, sizeof(gem_map));
82         gem_map.handle = bo->handles[0].u32;
83
84         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET,
85                        &gem_map);
86         if (ret) {
87                 fprintf(stderr,
88                         "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
89                 return MAP_FAILED;
90         }
91
92         return mmap(0, bo->sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED,
93                     bo->drv->fd, gem_map.offset);
94 }
95
96 const struct backend backend_rockchip =
97 {
98         .name = "rockchip",
99         .bo_create = rockchip_bo_create,
100         .bo_destroy = drv_gem_bo_destroy,
101         .bo_map = rockchip_bo_map,
102         .format_list = {
103                 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING},
104                 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR},
105                 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING},
106                 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR},
107                 {DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING},
108                 {DRV_FORMAT_NV12, DRV_BO_USE_SCANOUT | DRV_BO_USE_RENDERING},
109                 {DRV_FORMAT_NV12, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR},
110         }
111 };
112
113 #endif