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.
11 #include <rockchip_drm.h>
21 struct rockchip_private_map_data {
26 static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 DRM_FORMAT_BGR888, DRM_FORMAT_RGB565,
28 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 };
30 static const uint32_t texture_only_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_YVU420,
31 DRM_FORMAT_YVU420_ANDROID };
33 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
35 /* We've restricted ourselves to four bytes per pixel. */
36 const uint32_t pixel_size = 4;
38 const uint32_t clump_width = 4;
39 const uint32_t clump_height = 4;
43 const uint32_t block_width = 4 * clump_width;
44 const uint32_t block_height = 4 * clump_height;
46 const uint32_t block_width = 8 * clump_width;
47 const uint32_t block_height = 2 * clump_height;
50 const uint32_t header_block_size = 16;
51 const uint32_t body_block_size = block_width * block_height * pixel_size;
52 const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
53 const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
54 const uint32_t total_blocks = width_in_blocks * height_in_blocks;
56 const uint32_t header_plane_size = total_blocks * header_block_size;
57 const uint32_t body_plane_size = total_blocks * body_block_size;
59 /* GPU requires 64 bytes, but EGL import code expects 1024 byte
60 * alignement for the body plane. */
61 const uint32_t body_plane_alignment = 1024;
63 const uint32_t body_plane_offset = ALIGN(header_plane_size, body_plane_alignment);
64 const uint32_t total_size = body_plane_offset + body_plane_size;
66 bo->meta.strides[0] = width_in_blocks * block_width * pixel_size;
67 bo->meta.sizes[0] = total_size;
68 bo->meta.offsets[0] = 0;
70 bo->meta.total_size = total_size;
72 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
77 static int rockchip_init(struct driver *drv)
79 struct format_metadata metadata;
82 metadata.priority = 1;
83 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
85 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
86 &metadata, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
88 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
92 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
93 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
95 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
96 /* Camera ISP supports only NV12 output. */
97 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
98 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
99 BO_USE_HW_VIDEO_ENCODER | BO_USE_SCANOUT);
101 drv_modify_linear_combinations(drv);
103 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
106 drv_add_combination(drv, DRM_FORMAT_R8, &metadata,
107 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK |
108 BO_USE_LINEAR | BO_USE_PROTECTED);
113 static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
114 uint32_t format, const uint64_t *modifiers,
119 struct drm_rockchip_gem_create gem_create;
121 if (format == DRM_FORMAT_NV12) {
122 uint32_t w_mbs = DIV_ROUND_UP(width, 16);
123 uint32_t h_mbs = DIV_ROUND_UP(height, 16);
125 uint32_t aligned_width = w_mbs * 16;
126 uint32_t aligned_height = h_mbs * 16;
128 drv_bo_from_format(bo, aligned_width, aligned_height, format);
130 * drv_bo_from_format updates total_size. Add an extra data space for rockchip video
131 * driver to store motion vectors.
133 bo->meta.total_size += w_mbs * h_mbs * 128;
134 } else if (width <= 2560 &&
135 drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
136 /* If the caller has decided they can use AFBC, always
138 afbc_bo_from_format(bo, width, height, format);
140 if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
142 drv_log("no usable modifier found\n");
148 * Since the ARM L1 cache line size is 64 bytes, align to that
149 * as a performance optimization. For YV12, the Mali cmem allocator
150 * requires that chroma planes are aligned to 64-bytes, so align the
151 * luma plane to 128 bytes.
153 stride = drv_stride_from_format(format, width, 0);
154 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
155 stride = ALIGN(stride, 128);
157 stride = ALIGN(stride, 64);
159 drv_bo_from_format(bo, stride, height, format);
162 memset(&gem_create, 0, sizeof(gem_create));
163 gem_create.size = bo->meta.total_size;
165 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
168 drv_log("DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%" PRIu64 ")\n",
173 for (plane = 0; plane < bo->meta.num_planes; plane++)
174 bo->handles[plane].u32 = gem_create.handle;
179 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
182 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
183 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
184 ARRAY_SIZE(modifiers));
187 static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
190 struct drm_rockchip_gem_map_off gem_map;
191 struct rockchip_private_map_data *priv;
193 /* We can only map buffers created with SW access flags, which should
194 * have no modifiers (ie, not AFBC). */
195 if (bo->meta.format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
198 memset(&gem_map, 0, sizeof(gem_map));
199 gem_map.handle = bo->handles[0].u32;
201 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
203 drv_log("DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
207 void *addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
210 vma->length = bo->meta.total_size;
212 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
213 priv = calloc(1, sizeof(*priv));
214 priv->cached_addr = calloc(1, bo->meta.total_size);
215 priv->gem_addr = addr;
217 addr = priv->cached_addr;
223 static int rockchip_bo_unmap(struct bo *bo, struct vma *vma)
226 struct rockchip_private_map_data *priv = vma->priv;
227 vma->addr = priv->gem_addr;
228 free(priv->cached_addr);
233 return munmap(vma->addr, vma->length);
236 static int rockchip_bo_invalidate(struct bo *bo, struct mapping *mapping)
238 if (mapping->vma->priv) {
239 struct rockchip_private_map_data *priv = mapping->vma->priv;
240 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
246 static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping)
248 struct rockchip_private_map_data *priv = mapping->vma->priv;
249 if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
250 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
255 static uint32_t rockchip_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
258 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
259 /* Camera subsystem requires NV12. */
260 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
261 return DRM_FORMAT_NV12;
262 /*HACK: See b/28671744 */
263 return DRM_FORMAT_XBGR8888;
264 case DRM_FORMAT_FLEX_YCbCr_420_888:
265 return DRM_FORMAT_NV12;
271 const struct backend backend_rockchip = {
273 .init = rockchip_init,
274 .bo_create = rockchip_bo_create,
275 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
276 .bo_destroy = drv_gem_bo_destroy,
277 .bo_import = drv_prime_bo_import,
278 .bo_map = rockchip_bo_map,
279 .bo_unmap = rockchip_bo_unmap,
280 .bo_invalidate = rockchip_bo_invalidate,
281 .bo_flush = rockchip_bo_flush,
282 .resolve_format = rockchip_resolve_format,