OSDN Git Service

minigbm: Map and unmap correct size
[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         int ret;
25         size_t plane;
26         struct drm_rockchip_gem_create gem_create;
27
28         if (format == DRV_FORMAT_NV12) {
29                 width = ALIGN(width, 4);
30                 height = ALIGN(height, 4);
31         }
32
33         drv_bo_from_format(bo, width, height, format);
34
35         memset(&gem_create, 0, sizeof(gem_create));
36         gem_create.size = bo->total_size;
37
38         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE,
39                            &gem_create);
40
41         if (ret) {
42                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed "
43                                 "(size=%llu)\n", gem_create.size);
44                 return ret;
45         }
46
47         for (plane = 0; plane < bo->num_planes; plane++)
48                 bo->handles[plane].u32 = gem_create.handle;
49
50         return 0;
51 }
52
53 static void *rockchip_bo_map(struct bo *bo)
54 {
55         int ret;
56         struct drm_rockchip_gem_map_off gem_map;
57
58         memset(&gem_map, 0, sizeof(gem_map));
59         gem_map.handle = bo->handles[0].u32;
60
61         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET,
62                        &gem_map);
63         if (ret) {
64                 fprintf(stderr,
65                         "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
66                 return MAP_FAILED;
67         }
68
69         return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
70                     bo->drv->fd, gem_map.offset);
71 }
72
73 static drv_format_t rockchip_resolve_format(drv_format_t format)
74 {
75         switch (format) {
76         case DRV_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
77                 /*HACK: See b/28671744 */
78                 return DRV_FORMAT_XBGR8888;
79         case DRV_FORMAT_FLEX_YCbCr_420_888:
80                 return DRV_FORMAT_NV12;
81         default:
82                 return format;
83         }
84 }
85
86 const struct backend backend_rockchip =
87 {
88         .name = "rockchip",
89         .bo_create = rockchip_bo_create,
90         .bo_destroy = drv_gem_bo_destroy,
91         .bo_map = rockchip_bo_map,
92         .resolve_format = rockchip_resolve_format,
93         .format_list = {
94                 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
95                                       DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
96                                       DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
97                                       DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
98                 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
99                                       DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
100                 {DRV_FORMAT_XBGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
101                                       DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
102                                       DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
103                                       DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
104                 {DRV_FORMAT_XBGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
105                                       DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
106                 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
107                                       DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
108                                       DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
109                                       DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
110                 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
111                                       DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
112                 {DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
113                                       DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
114                                       DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
115                                       DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
116                 {DRV_FORMAT_NV12,     DRV_BO_USE_SCANOUT | DRV_BO_USE_RENDERING |
117                                       DRV_BO_USE_HW_TEXTURE | DRV_BO_USE_HW_RENDER |
118                                       DRV_BO_USE_HW_2D | DRV_BO_USE_SW_READ_RARELY |
119                                       DRV_BO_USE_SW_WRITE_RARELY},
120                 {DRV_FORMAT_NV12,     DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR |
121                                       DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
122                 {DRV_FORMAT_YVU420,   DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
123                                       DRV_BO_USE_SW_WRITE_OFTEN},
124         }
125 };
126
127 #endif