OSDN Git Service

minigbm: work with strides, not widths
[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 <stdbool.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 const uint32_t supported_formats[] = {
22         DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_NV12,
23         DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888,
24         DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID
25 };
26
27 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
28                                uint32_t format)
29 {
30         /* We've restricted ourselves to four bytes per pixel. */
31         const uint32_t pixel_size = 4;
32
33         const uint32_t clump_width = 4;
34         const uint32_t clump_height = 4;
35
36 #define AFBC_NARROW 1
37 #if AFBC_NARROW == 1
38         const uint32_t block_width = 4 * clump_width;
39         const uint32_t block_height = 4 * clump_height;
40 #else
41         const uint32_t block_width = 8 * clump_width;
42         const uint32_t block_height = 2 * clump_height;
43 #endif
44
45         const uint32_t header_block_size = 16;
46         const uint32_t body_block_size = block_width * block_height * pixel_size;
47         const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
48         const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
49         const uint32_t total_blocks = width_in_blocks * height_in_blocks;
50
51         const uint32_t header_plane_size = total_blocks * header_block_size;
52         const uint32_t body_plane_size = total_blocks * body_block_size;
53
54         /* GPU requires 64 bytes, but EGL import code expects 1024 byte
55          * alignement for the body plane. */
56         const uint32_t body_plane_alignment = 1024;
57
58         const uint32_t body_plane_offset =
59                 ALIGN(header_plane_size, body_plane_alignment);
60         const uint32_t total_size =
61                 body_plane_offset + body_plane_size;
62
63         bo->strides[0] = width_in_blocks * block_width * pixel_size;
64         bo->sizes[0] = total_size;
65         bo->offsets[0] = 0;
66
67         bo->total_size = total_size;
68
69         bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
70
71         return 0;
72 }
73
74 static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
75 {
76         int ret;
77         uint32_t i;
78         uint64_t flags;
79         struct combination *combo;
80         struct format_metadata metadata;
81
82         for (i = 0; i < drv->backend->combos.size; i++) {
83                 combo = &drv->backend->combos.data[i];
84                 if (combo->format == item->format) {
85                         if (item->modifier ==
86                             DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
87                                 flags = BO_USE_RENDERING | BO_USE_SCANOUT |
88                                         BO_USE_TEXTURE;
89                                 metadata.modifier = item->modifier;
90                                 metadata.tiling = 0;
91                                 metadata.priority = 2;
92
93                                 ret = drv_add_combination(drv, item[i].format,
94                                                           &metadata, flags);
95                                 if (ret)
96                                         return ret;
97                         } else {
98                                 combo->usage |= item->usage;
99                         }
100                 }
101         }
102
103         return 0;
104 }
105
106 static int rockchip_init(struct driver *drv)
107 {
108         int ret;
109         uint32_t i, num_items;
110         struct kms_item *items;
111         struct format_metadata metadata;
112
113         metadata.tiling = 0;
114         metadata.priority = 1;
115         metadata.modifier = DRM_FORMAT_MOD_NONE;
116
117         ret = drv_add_combinations(drv, supported_formats,
118                                    ARRAY_SIZE(supported_formats), &metadata,
119                                    BO_COMMON_USE_MASK);
120         if (ret)
121                 return ret;
122
123         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
124                                BO_USE_CURSOR | BO_USE_SCANOUT);
125         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
126                                BO_USE_CURSOR | BO_USE_SCANOUT);
127
128         items = drv_query_kms(drv, &num_items);
129         if (!items || !num_items)
130                 return 0;
131
132         for (i = 0; i < num_items; i++) {
133                 ret = rockchip_add_kms_item(drv, &items[i]);
134                 if (ret) {
135                         free(items);
136                         return ret;
137                 }
138         }
139
140         free(items);
141         return 0;
142 }
143
144 static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
145 {
146         uint32_t i;
147
148         for (i = 0; i < count; i++)
149                 if (list[i] == modifier)
150                         return true;
151
152         return false;
153 }
154
155 static int rockchip_bo_create_with_modifiers(struct bo *bo,
156                                              uint32_t width, uint32_t height,
157                                              uint32_t format,
158                                              const uint64_t *modifiers,
159                                              uint32_t count)
160 {
161         int ret;
162         size_t plane;
163         struct drm_rockchip_gem_create gem_create;
164
165         if (format == DRM_FORMAT_NV12) {
166                 uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
167                 uint32_t h_mbs = DIV_ROUND_UP(ALIGN(height, 16), 16);
168
169                 uint32_t aligned_width = w_mbs * 16;
170                 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
171
172                 drv_bo_from_format(bo, aligned_width, height, format);
173                 bo->total_size = bo->strides[0] * aligned_height
174                                  + w_mbs * h_mbs * 128;
175         } else if (width <= 2560 &&
176                    has_modifier(modifiers, count,
177                                 DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
178                 /* If the caller has decided they can use AFBC, always
179                  * pick that */
180                 afbc_bo_from_format(bo, width, height, format);
181         } else {
182                 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
183                         errno = EINVAL;
184                         fprintf(stderr, "no usable modifier found\n");
185                         return -1;
186                 }
187
188                 uint32_t stride;
189                 /*
190                  * Since the ARM L1 cache line size is 64 bytes, align to that
191                  * as a performance optimization.
192                  */
193                 stride = drv_stride_from_format(format, width, 0);
194                 stride = ALIGN(stride, 64);
195                 drv_bo_from_format(bo, stride, height, format);
196         }
197
198         memset(&gem_create, 0, sizeof(gem_create));
199         gem_create.size = bo->total_size;
200
201         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE,
202                        &gem_create);
203
204         if (ret) {
205                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed "
206                                 "(size=%llu)\n", gem_create.size);
207                 return ret;
208         }
209
210         for (plane = 0; plane < bo->num_planes; plane++)
211                 bo->handles[plane].u32 = gem_create.handle;
212
213         return 0;
214 }
215
216 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height,
217                               uint32_t format, uint32_t flags)
218 {
219         uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
220
221         return rockchip_bo_create_with_modifiers(bo, width, height, format,
222                                                  modifiers, ARRAY_SIZE(modifiers));
223 }
224
225 static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane)
226 {
227         int ret;
228         struct drm_rockchip_gem_map_off gem_map;
229
230         /* We can only map buffers created with SW access flags, which should
231          * have no modifiers (ie, not AFBC). */
232         if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
233                 return MAP_FAILED;
234
235         memset(&gem_map, 0, sizeof(gem_map));
236         gem_map.handle = bo->handles[0].u32;
237
238         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET,
239                        &gem_map);
240         if (ret) {
241                 fprintf(stderr,
242                         "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
243                 return MAP_FAILED;
244         }
245
246         data->length = bo->total_size;
247
248         return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
249                     bo->drv->fd, gem_map.offset);
250 }
251
252 static uint32_t rockchip_resolve_format(uint32_t format)
253 {
254         switch (format) {
255         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
256                 /*HACK: See b/28671744 */
257                 return DRM_FORMAT_XBGR8888;
258         case DRM_FORMAT_FLEX_YCbCr_420_888:
259                 return DRM_FORMAT_NV12;
260         default:
261                 return format;
262         }
263 }
264
265 struct backend backend_rockchip =
266 {
267         .name = "rockchip",
268         .init = rockchip_init,
269         .bo_create = rockchip_bo_create,
270         .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
271         .bo_destroy = drv_gem_bo_destroy,
272         .bo_import = drv_prime_bo_import,
273         .bo_map = rockchip_bo_map,
274         .resolve_format = rockchip_resolve_format,
275 };
276
277 #endif