OSDN Git Service

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