OSDN Git Service

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