forked from projectceladon/minigbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediatek.c
303 lines (252 loc) · 8.84 KB
/
mediatek.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifdef DRV_MEDIATEK
// clang-format off
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <xf86drm.h>
#include <mediatek_drm.h>
// clang-format on
#include "drv_priv.h"
#include "helpers.h"
#include "util.h"
#define TILE_TYPE_LINEAR 0
struct mediatek_private_map_data {
void *cached_addr;
void *gem_addr;
int prime_fd;
};
static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
DRM_FORMAT_XRGB8888 };
#ifdef MTK_MT8183
static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV21, DRM_FORMAT_NV12,
DRM_FORMAT_YUYV, DRM_FORMAT_YVU420,
DRM_FORMAT_YVU420_ANDROID };
#else
static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
DRM_FORMAT_NV12 };
#endif
static int mediatek_init(struct driver *drv)
{
struct format_metadata metadata;
drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
&LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
&LINEAR_METADATA, BO_USE_TEXTURE_MASK);
drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
BO_USE_SW_MASK | BO_USE_LINEAR | BO_USE_PROTECTED);
/*
* Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
* Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
*/
drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
/* Android CTS tests require this. */
drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
/* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
metadata.tiling = TILE_TYPE_LINEAR;
metadata.priority = 1;
metadata.modifier = DRM_FORMAT_MOD_LINEAR;
drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER);
drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER);
drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER);
#ifdef MTK_MT8183
/* Only for MT8183 Camera subsystem */
drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
/* Private formats for private reprocessing in camera */
drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK);
#endif
return drv_modify_linear_combinations(drv);
}
static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
uint32_t format, const uint64_t *modifiers,
uint32_t count)
{
int ret;
size_t plane;
uint32_t stride;
struct drm_mtk_gem_create gem_create;
if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
errno = EINVAL;
drv_log("no usable modifier found\n");
return -EINVAL;
}
/*
* Since the ARM L1 cache line size is 64 bytes, align to that as a
* performance optimization.
*/
stride = drv_stride_from_format(format, width, 0);
stride = ALIGN(stride, 64);
if (bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER) {
uint32_t aligned_height = ALIGN(height, 32);
uint32_t padding[DRV_MAX_PLANES] = { 0 };
for (plane = 0; plane < bo->meta.num_planes; ++plane) {
uint32_t plane_stride = drv_stride_from_format(format, stride, plane);
padding[plane] = plane_stride *
(32 / drv_vertical_subsampling_from_format(format, plane));
}
drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding);
} else {
#ifdef MTK_MT8183
/*
* JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer
* from camera can be put in JEA directly so align the height to 16
* bytes.
*/
if (format == DRM_FORMAT_NV12)
height = ALIGN(height, 16);
#endif
drv_bo_from_format(bo, stride, height, format);
}
memset(&gem_create, 0, sizeof(gem_create));
gem_create.size = bo->meta.total_size;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
if (ret) {
drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
return -errno;
}
for (plane = 0; plane < bo->meta.num_planes; plane++)
bo->handles[plane].u32 = gem_create.handle;
return 0;
}
static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
uint64_t use_flags)
{
uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
ARRAY_SIZE(modifiers));
}
static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
{
int ret, prime_fd;
struct drm_mtk_gem_map_off gem_map;
struct mediatek_private_map_data *priv;
memset(&gem_map, 0, sizeof(gem_map));
gem_map.handle = bo->handles[0].u32;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
if (ret) {
drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
return MAP_FAILED;
}
prime_fd = drv_bo_get_plane_fd(bo, 0);
if (prime_fd < 0) {
drv_log("Failed to get a prime fd\n");
return MAP_FAILED;
}
void *addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
gem_map.offset);
vma->length = bo->meta.total_size;
priv = calloc(1, sizeof(*priv));
priv->prime_fd = prime_fd;
vma->priv = priv;
if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
priv->cached_addr = calloc(1, bo->meta.total_size);
priv->gem_addr = addr;
addr = priv->cached_addr;
}
return addr;
}
static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
{
if (vma->priv) {
struct mediatek_private_map_data *priv = vma->priv;
if (priv->cached_addr) {
vma->addr = priv->gem_addr;
free(priv->cached_addr);
}
close(priv->prime_fd);
free(priv);
vma->priv = NULL;
}
return munmap(vma->addr, vma->length);
}
static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
{
struct mediatek_private_map_data *priv = mapping->vma->priv;
if (priv) {
struct pollfd fds = {
.fd = priv->prime_fd,
};
if (mapping->vma->map_flags & BO_MAP_WRITE)
fds.events |= POLLOUT;
if (mapping->vma->map_flags & BO_MAP_READ)
fds.events |= POLLIN;
poll(&fds, 1, -1);
if (fds.revents != fds.events)
drv_log("poll prime_fd failed\n");
if (priv->cached_addr)
memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
}
return 0;
}
static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
{
struct mediatek_private_map_data *priv = mapping->vma->priv;
if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
return 0;
}
static uint32_t mediatek_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
{
switch (format) {
case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
#ifdef MTK_MT8183
/* Only MT8183 Camera subsystem offers private reprocessing
* capability. CAMERA_READ indicates the buffer is intended for
* reprocessing and hence given the private format for MTK. */
if (use_flags & BO_USE_CAMERA_READ)
return DRM_FORMAT_MTISP_SXYZW10;
/* For non-reprocessing uses, only MT8183 Camera subsystem
* requires NV12. */
else if (use_flags & BO_USE_CAMERA_WRITE)
return DRM_FORMAT_NV12;
#endif
/*HACK: See b/28671744 */
return DRM_FORMAT_XBGR8888;
case DRM_FORMAT_FLEX_YCbCr_420_888:
#ifdef MTK_MT8183
/* MT8183 camera and decoder subsystems require NV12. */
if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER)) {
return DRM_FORMAT_NV12;
}
#endif
return DRM_FORMAT_YVU420;
default:
return format;
}
}
const struct backend backend_mediatek = {
.name = "mediatek",
.init = mediatek_init,
.bo_create = mediatek_bo_create,
.bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
.bo_destroy = drv_gem_bo_destroy,
.bo_import = drv_prime_bo_import,
.bo_map = mediatek_bo_map,
.bo_unmap = mediatek_bo_unmap,
.bo_invalidate = mediatek_bo_invalidate,
.bo_flush = mediatek_bo_flush,
.resolve_format = mediatek_resolve_format,
};
#endif