OSDN Git Service

rockchip: Fix the size computation with NV12 buffer
[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 <stdio.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <xf86drm.h>
15
16 #include "drv_priv.h"
17 #include "helpers.h"
18 #include "util.h"
19
20 struct rockchip_private_map_data {
21         void *cached_addr;
22         void *gem_addr;
23 };
24
25 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
26                                                   DRM_FORMAT_BGR888,   DRM_FORMAT_RGB565,
27                                                   DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 };
28
29 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12,
30                                                    DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
31
32 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
33 {
34         /* We've restricted ourselves to four bytes per pixel. */
35         const uint32_t pixel_size = 4;
36
37         const uint32_t clump_width = 4;
38         const uint32_t clump_height = 4;
39
40 #define AFBC_NARROW 1
41 #if AFBC_NARROW == 1
42         const uint32_t block_width = 4 * clump_width;
43         const uint32_t block_height = 4 * clump_height;
44 #else
45         const uint32_t block_width = 8 * clump_width;
46         const uint32_t block_height = 2 * clump_height;
47 #endif
48
49         const uint32_t header_block_size = 16;
50         const uint32_t body_block_size = block_width * block_height * pixel_size;
51         const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
52         const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
53         const uint32_t total_blocks = width_in_blocks * height_in_blocks;
54
55         const uint32_t header_plane_size = total_blocks * header_block_size;
56         const uint32_t body_plane_size = total_blocks * body_block_size;
57
58         /* GPU requires 64 bytes, but EGL import code expects 1024 byte
59          * alignement for the body plane. */
60         const uint32_t body_plane_alignment = 1024;
61
62         const uint32_t body_plane_offset = ALIGN(header_plane_size, body_plane_alignment);
63         const uint32_t total_size = body_plane_offset + body_plane_size;
64
65         bo->strides[0] = width_in_blocks * block_width * pixel_size;
66         bo->sizes[0] = total_size;
67         bo->offsets[0] = 0;
68
69         bo->total_size = total_size;
70
71         bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
72
73         return 0;
74 }
75
76 static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
77 {
78         uint32_t i, j;
79         uint64_t use_flags;
80         struct combination *combo;
81         struct format_metadata metadata;
82
83         for (i = 0; i < drv_array_size(drv->combos); i++) {
84                 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
85                 if (combo->format == item->format) {
86                         if (item->modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
87                                 use_flags = BO_USE_RENDERING | BO_USE_SCANOUT | BO_USE_TEXTURE;
88                                 metadata.modifier = item->modifier;
89                                 metadata.tiling = 0;
90                                 metadata.priority = 2;
91
92                                 for (j = 0; j < ARRAY_SIZE(texture_source_formats); j++) {
93                                         if (item->format == texture_source_formats[j])
94                                                 use_flags &= ~BO_USE_RENDERING;
95                                 }
96
97                                 drv_add_combinations(drv, &item->format, 1, &metadata, use_flags);
98                         } else {
99                                 combo->use_flags |= item->use_flags;
100                         }
101                 }
102         }
103
104         return 0;
105 }
106
107 static int rockchip_init(struct driver *drv)
108 {
109         int ret;
110         uint32_t i;
111         struct drv_array *kms_items;
112         struct format_metadata metadata;
113
114         metadata.tiling = 0;
115         metadata.priority = 1;
116         metadata.modifier = DRM_FORMAT_MOD_LINEAR;
117
118         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
119                              &metadata, BO_USE_RENDER_MASK);
120
121         drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
122                              &metadata, BO_USE_TEXTURE_MASK);
123
124         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
125         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
126
127         /* Camera ISP supports only NV12 output. */
128         drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
129                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER);
130         /*
131          * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
132          * from camera.
133          */
134         drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
135                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
136
137         kms_items = drv_query_kms(drv);
138         if (!kms_items)
139                 return 0;
140
141         for (i = 0; i < drv_array_size(kms_items); i++) {
142                 ret = rockchip_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
143                 if (ret) {
144                         drv_array_destroy(kms_items);
145                         return ret;
146                 }
147         }
148
149         drv_array_destroy(kms_items);
150         return 0;
151 }
152
153 static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
154                                              uint32_t format, const uint64_t *modifiers,
155                                              uint32_t count)
156 {
157         int ret;
158         size_t plane;
159         struct drm_rockchip_gem_create gem_create;
160
161         if (format == DRM_FORMAT_NV12) {
162                 uint32_t w_mbs = DIV_ROUND_UP(width, 16);
163                 uint32_t h_mbs = DIV_ROUND_UP(height, 16);
164
165                 uint32_t aligned_width = w_mbs * 16;
166                 uint32_t aligned_height = h_mbs * 16;
167
168                 drv_bo_from_format(bo, aligned_width, aligned_height, format);
169                 /*
170                  * drv_bo_from_format updates total_size. Add an extra data space for rockchip video
171                  * driver to store motion vectors.
172                  */
173                 bo->total_size += w_mbs * h_mbs * 128;
174         } else if (width <= 2560 &&
175                    drv_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 (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
181                         errno = EINVAL;
182                         drv_log("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                 drv_log("DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%llu)\n",
209                         (unsigned long long)gem_create.size);
210                 return -errno;
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                               uint64_t use_flags)
221 {
222         uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
223         return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
224                                                  ARRAY_SIZE(modifiers));
225 }
226
227 static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
228 {
229         int ret;
230         struct drm_rockchip_gem_map_off gem_map;
231         struct rockchip_private_map_data *priv;
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                 drv_log("DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
244                 return MAP_FAILED;
245         }
246
247         void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
248                           gem_map.offset);
249
250         vma->length = bo->total_size;
251
252         if (bo->use_flags & BO_USE_RENDERSCRIPT) {
253                 priv = calloc(1, sizeof(*priv));
254                 priv->cached_addr = calloc(1, bo->total_size);
255                 priv->gem_addr = addr;
256                 vma->priv = priv;
257                 addr = priv->cached_addr;
258         }
259
260         return addr;
261 }
262
263 static int rockchip_bo_unmap(struct bo *bo, struct vma *vma)
264 {
265         if (vma->priv) {
266                 struct rockchip_private_map_data *priv = vma->priv;
267                 vma->addr = priv->gem_addr;
268                 free(priv->cached_addr);
269                 free(priv);
270                 vma->priv = NULL;
271         }
272
273         return munmap(vma->addr, vma->length);
274 }
275
276 static int rockchip_bo_invalidate(struct bo *bo, struct mapping *mapping)
277 {
278         if (mapping->vma->priv) {
279                 struct rockchip_private_map_data *priv = mapping->vma->priv;
280                 memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
281         }
282
283         return 0;
284 }
285
286 static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping)
287 {
288         struct rockchip_private_map_data *priv = mapping->vma->priv;
289         if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
290                 memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
291
292         return 0;
293 }
294
295 static uint32_t rockchip_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
296 {
297         switch (format) {
298         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
299                 /* Camera subsystem requires NV12. */
300                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
301                         return DRM_FORMAT_NV12;
302                 /*HACK: See b/28671744 */
303                 return DRM_FORMAT_XBGR8888;
304         case DRM_FORMAT_FLEX_YCbCr_420_888:
305                 return DRM_FORMAT_NV12;
306         default:
307                 return format;
308         }
309 }
310
311 const struct backend backend_rockchip = {
312         .name = "rockchip",
313         .init = rockchip_init,
314         .bo_create = rockchip_bo_create,
315         .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
316         .bo_destroy = drv_gem_bo_destroy,
317         .bo_import = drv_prime_bo_import,
318         .bo_map = rockchip_bo_map,
319         .bo_unmap = rockchip_bo_unmap,
320         .bo_invalidate = rockchip_bo_invalidate,
321         .bo_flush = rockchip_bo_flush,
322         .resolve_format = rockchip_resolve_format,
323 };
324
325 #endif