OSDN Git Service

minigbm: add bo import callback
[android-x86/external-minigbm.git] / rockchip.c
1 /*
2  * Copyright 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 <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <xf86drm.h>
14 #include <rockchip_drm.h>
15
16 #include "drv_priv.h"
17 #include "helpers.h"
18 #include "util.h"
19
20 static struct supported_combination combos[11] = {
21         {DRM_FORMAT_ABGR8888, DRM_FORMAT_MOD_NONE,
22                 BO_USE_RENDERING | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN |
23                 BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
24         {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
25                 BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
26         {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
27                 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
28         {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE,
29                 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
30         {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE,
31                 BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
32         {DRM_FORMAT_RGB565, DRM_FORMAT_MOD_NONE,
33                 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
34         {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE,
35                 BO_USE_RENDERING | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN |
36                 BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
37         {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE,
38                 BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
39         {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
40                 BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
41         {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
42                 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
43         {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE,
44                 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
45 };
46
47 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
48                                uint32_t format)
49 {
50         /* We've restricted ourselves to four bytes per pixel. */
51         const uint32_t pixel_size = 4;
52
53         const uint32_t clump_width = 4;
54         const uint32_t clump_height = 4;
55
56 #define AFBC_NARROW 1
57 #if AFBC_NARROW == 1
58         const uint32_t block_width = 4 * clump_width;
59         const uint32_t block_height = 4 * clump_height;
60 #else
61         const uint32_t block_width = 8 * clump_width;
62         const uint32_t block_height = 2 * clump_height;
63 #endif
64
65         const uint32_t header_block_size = 16;
66         const uint32_t body_block_size = block_width * block_height * pixel_size;
67         const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
68         const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
69         const uint32_t total_blocks = width_in_blocks * height_in_blocks;
70
71         const uint32_t header_plane_size = total_blocks * header_block_size;
72         const uint32_t body_plane_size = total_blocks * body_block_size;
73
74         /* GPU requires 64 bytes, but EGL import code expects 1024 byte
75          * alignement for the body plane. */
76         const uint32_t body_plane_alignment = 1024;
77
78         const uint32_t body_plane_offset =
79                 ALIGN(header_plane_size, body_plane_alignment);
80         const uint32_t total_size =
81                 body_plane_offset + body_plane_size;
82
83         bo->strides[0] = width_in_blocks * block_width * pixel_size;
84         bo->sizes[0] = total_size;
85         bo->offsets[0] = 0;
86
87         bo->total_size = total_size;
88
89         bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
90
91         return 0;
92 }
93
94 static int rockchip_init(struct driver *drv)
95 {
96         drv_insert_combinations(drv, combos, ARRAY_SIZE(combos));
97         return drv_add_kms_flags(drv);
98 }
99
100 static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
101 {
102         uint32_t i;
103
104         for (i = 0; i < count; i++)
105                 if (list[i] == modifier)
106                         return true;
107
108         return false;
109 }
110
111 static int rockchip_bo_create_with_modifiers(struct bo *bo,
112                                              uint32_t width, uint32_t height,
113                                              uint32_t format,
114                                              const uint64_t *modifiers,
115                                              uint32_t count)
116 {
117         int ret;
118         size_t plane;
119         struct drm_rockchip_gem_create gem_create;
120
121         if (format == DRM_FORMAT_NV12) {
122                 uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
123                 uint32_t h_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
124
125                 uint32_t aligned_width = w_mbs * 16;
126                 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
127
128                 drv_bo_from_format(bo, aligned_width, height, format);
129                 bo->total_size = bo->strides[0] * aligned_height
130                                  + w_mbs * h_mbs * 128;
131         } else if (has_modifier(modifiers, count,
132                                 DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
133                 /* If the caller has decided they can use AFBC, always
134                  * pick that */
135                 afbc_bo_from_format(bo, width, height, format);
136         } else {
137                 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
138                         errno = EINVAL;
139                         fprintf(stderr, "no usable modifier found\n");
140                         return -1;
141                 }
142                 drv_bo_from_format(bo, width, height, format);
143         }
144
145         memset(&gem_create, 0, sizeof(gem_create));
146         gem_create.size = bo->total_size;
147
148         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE,
149                        &gem_create);
150
151         if (ret) {
152                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed "
153                                 "(size=%llu)\n", gem_create.size);
154                 return ret;
155         }
156
157         for (plane = 0; plane < bo->num_planes; plane++)
158                 bo->handles[plane].u32 = gem_create.handle;
159
160         return 0;
161 }
162
163 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height,
164                               uint32_t format, uint32_t flags)
165 {
166         uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
167
168         return rockchip_bo_create_with_modifiers(bo, width, height, format,
169                                                  modifiers, ARRAY_SIZE(modifiers));
170 }
171
172 static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane)
173 {
174         int ret;
175         struct drm_rockchip_gem_map_off gem_map;
176
177         /* We can only map buffers created with SW access flags, which should
178          * have no modifiers (ie, not AFBC). */
179         if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
180                 return MAP_FAILED;
181
182         memset(&gem_map, 0, sizeof(gem_map));
183         gem_map.handle = bo->handles[0].u32;
184
185         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET,
186                        &gem_map);
187         if (ret) {
188                 fprintf(stderr,
189                         "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
190                 return MAP_FAILED;
191         }
192
193         data->length = bo->total_size;
194
195         return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
196                     bo->drv->fd, gem_map.offset);
197 }
198
199 static uint32_t rockchip_resolve_format(uint32_t format)
200 {
201         switch (format) {
202         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
203                 /*HACK: See b/28671744 */
204                 return DRM_FORMAT_XBGR8888;
205         case DRM_FORMAT_FLEX_YCbCr_420_888:
206                 return DRM_FORMAT_NV12;
207         default:
208                 return format;
209         }
210 }
211
212 struct backend backend_rockchip =
213 {
214         .name = "rockchip",
215         .init = rockchip_init,
216         .bo_create = rockchip_bo_create,
217         .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
218         .bo_destroy = drv_gem_bo_destroy,
219         .bo_import = drv_prime_bo_import,
220         .bo_map = rockchip_bo_map,
221         .resolve_format = rockchip_resolve_format,
222 };
223
224 #endif