OSDN Git Service

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