OSDN Git Service

minigbm: add the BO_USE_RENDERSCRIPT flag back in
[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 render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
22                                                   DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
23                                                   DRM_FORMAT_XRGB8888 };
24
25 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12,
26                                                    DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
27
28 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, 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 = ALIGN(header_plane_size, body_plane_alignment);
59         const uint32_t total_size = body_plane_offset + body_plane_size;
60
61         bo->strides[0] = width_in_blocks * block_width * pixel_size;
62         bo->sizes[0] = total_size;
63         bo->offsets[0] = 0;
64
65         bo->total_size = total_size;
66
67         bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
68
69         return 0;
70 }
71
72 static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
73 {
74         int ret;
75         uint32_t i, j;
76         uint64_t flags;
77         struct combination *combo;
78         struct format_metadata metadata;
79
80         for (i = 0; i < drv->backend->combos.size; i++) {
81                 combo = &drv->backend->combos.data[i];
82                 if (combo->format == item->format) {
83                         if (item->modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
84                                 flags = BO_USE_RENDERING | BO_USE_SCANOUT | BO_USE_TEXTURE;
85                                 metadata.modifier = item->modifier;
86                                 metadata.tiling = 0;
87                                 metadata.priority = 2;
88
89                                 for (j = 0; j < ARRAY_SIZE(texture_source_formats); j++) {
90                                         if (item->format == texture_source_formats[j])
91                                                 flags &= ~BO_USE_RENDERING;
92                                 }
93
94                                 ret = drv_add_combination(drv, item[i].format, &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, render_target_formats, ARRAY_SIZE(render_target_formats),
118                                    &metadata, BO_USE_RENDER_MASK);
119         if (ret)
120                 return ret;
121
122         ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
123                                    &metadata, BO_USE_TEXTURE_MASK);
124         if (ret)
125                 return ret;
126
127         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
128         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
129
130         items = drv_query_kms(drv, &num_items);
131         if (!items || !num_items)
132                 return 0;
133
134         for (i = 0; i < num_items; i++) {
135                 ret = rockchip_add_kms_item(drv, &items[i]);
136                 if (ret) {
137                         free(items);
138                         return ret;
139                 }
140         }
141
142         free(items);
143         return 0;
144 }
145
146 static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
147 {
148         uint32_t i;
149
150         for (i = 0; i < count; i++)
151                 if (list[i] == modifier)
152                         return true;
153
154         return false;
155 }
156
157 static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
158                                              uint32_t format, 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 + w_mbs * h_mbs * 128;
174         } else if (width <= 2560 &&
175                    has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
176                 /* If the caller has decided they can use AFBC, always
177                  * pick that */
178                 afbc_bo_from_format(bo, width, height, format);
179         } else {
180                 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
181                         errno = EINVAL;
182                         fprintf(stderr, "no usable modifier found\n");
183                         return -1;
184                 }
185
186                 uint32_t stride;
187                 /*
188                  * Since the ARM L1 cache line size is 64 bytes, align to that
189                  * as a performance optimization. For YV12, the Mali cmem allocator
190                  * requires that chroma planes are aligned to 64-bytes, so align the
191                  * luma plane to 128 bytes.
192                  */
193                 stride = drv_stride_from_format(format, width, 0);
194                 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
195                         stride = ALIGN(stride, 128);
196                 else
197                         stride = ALIGN(stride, 64);
198
199                 drv_bo_from_format(bo, stride, height, format);
200         }
201
202         memset(&gem_create, 0, sizeof(gem_create));
203         gem_create.size = bo->total_size;
204
205         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
206
207         if (ret) {
208                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%llu)\n",
209                         gem_create.size);
210                 return ret;
211         }
212
213         for (plane = 0; plane < bo->num_planes; plane++)
214                 bo->handles[plane].u32 = gem_create.handle;
215
216         return 0;
217 }
218
219 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
220                               uint32_t flags)
221 {
222         uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
223
224         return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
225                                                  ARRAY_SIZE(modifiers));
226 }
227
228 static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
229 {
230         int ret;
231         struct drm_rockchip_gem_map_off gem_map;
232
233         /* We can only map buffers created with SW access flags, which should
234          * have no modifiers (ie, not AFBC). */
235         if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
236                 return MAP_FAILED;
237
238         memset(&gem_map, 0, sizeof(gem_map));
239         gem_map.handle = bo->handles[0].u32;
240
241         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
242         if (ret) {
243                 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
244                 return MAP_FAILED;
245         }
246
247         data->length = bo->total_size;
248
249         return mmap(0, bo->total_size, prot, MAP_SHARED, bo->drv->fd, gem_map.offset);
250 }
251
252 static uint32_t rockchip_resolve_format(uint32_t format, uint64_t usage)
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         .name = "rockchip",
267         .init = rockchip_init,
268         .bo_create = rockchip_bo_create,
269         .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
270         .bo_destroy = drv_gem_bo_destroy,
271         .bo_import = drv_prime_bo_import,
272         .bo_map = rockchip_bo_map,
273         .resolve_format = rockchip_resolve_format,
274 };
275
276 #endif