[drm]: add android drm allocator support

1. enable drm allocator in android
2. modify size calculating when alloc

Todo: wait for new interface of kernel to decide
use whether drm or ion as allocator

Change-Id: Ia6791d91d113b238be77088ae747012fb786d683
Signed-off-by: Jung Zhao <jung.zhao@rock-chips.com>
This commit is contained in:
Jung Zhao
2016-08-24 16:53:10 +08:00
parent e8a2c929ce
commit 70e2ebdb2b
6 changed files with 1450 additions and 1329 deletions

View File

@@ -1,7 +1,7 @@
# vim: syntax=cmake # vim: syntax=cmake
if(${ANDROID}) if(${ANDROID})
set(OS_DIR android) set(OS_DIR android)
set(MPP_ALLOCATOR allocator/allocator_ion.c) set(MPP_ALLOCATOR allocator/allocator_ion.c allocator/allocator_drm.c)
elseif(${UNIX}) elseif(${UNIX})
set(OS_DIR linux) set(OS_DIR linux)
set(MPP_ALLOCATOR allocator/allocator_ion.c allocator/allocator_drm.c) set(MPP_ALLOCATOR allocator/allocator_ion.c allocator/allocator_drm.c)

View File

@@ -1,388 +1,481 @@
/* /*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD * Copyright 2010 Rockchip Electronics S.LSI Co. LTD
* *
* Licensed under the Apache License, Versdrm 2.0 (the "License"); * Licensed under the Apache License, Versdrm 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITDRMS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITDRMS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissdrms and * See the License for the specific language governing permissdrms and
* limitatdrms under the License. * limitatdrms under the License.
*/ */
#define MODULE_TAG "mpp_drm" #define MODULE_TAG "mpp_drm"
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <linux/drm.h> #include <linux/drm.h>
#include <linux/drm_mode.h> #include <linux/drm_mode.h>
#include "os_mem.h" #include "os_mem.h"
#include "allocator_drm.h" #include "allocator_drm.h"
#include "mpp_mem.h" #include "mpp_mem.h"
#include "mpp_log.h" #include "mpp_log.h"
#include "mpp_common.h" #include "mpp_common.h"
static RK_U32 drm_debug = 0; static RK_U32 drm_debug = 0;
#define DRM_FUNCTDRM (0x00000001) #define DRM_FUNCTION (0x00000001)
#define DRM_DEVICE (0x00000002) #define DRM_DEVICE (0x00000002)
#define DRM_CLINET (0x00000004) #define DRM_CLIENT (0x00000004)
#define DRM_IOCTL (0x00000008) #define DRM_IOCTL (0x00000008)
#define DRM_DETECT_IOMMU_DISABLE (0x0) /* use DRM_HEAP_TYPE_DMA */ #define DRM_DETECT_IOMMU_DISABLE (0x0) /* use DRM_HEAP_TYPE_DMA */
#define DRM_DETECT_IOMMU_ENABLE (0x1) /* use DRM_HEAP_TYPE_SYSTEM */ #define DRM_DETECT_IOMMU_ENABLE (0x1) /* use DRM_HEAP_TYPE_SYSTEM */
#define DRM_DETECT_NO_DTS (0x2) /* use DRM_HEAP_TYPE_CARVEOUT */ #define DRM_DETECT_NO_DTS (0x2) /* use DRM_HEAP_TYPE_CARVEOUT */
#define drm_dbg(flag, fmt, ...) _mpp_dbg(drm_debug, flag, fmt, ## __VA_ARGS__) #define drm_dbg(flag, fmt, ...) _mpp_dbg_f(drm_debug, flag, fmt, ## __VA_ARGS__)
static int drm_ioctl(int fd, int req, void *arg) static int drm_ioctl(int fd, int req, void *arg)
{ {
int ret = ioctl(fd, req, arg); int ret;
if (ret < 0) {
mpp_err("drm_ioctl %x failed with code %d: %s\n", req, do {
ret, strerror(errno)); ret = ioctl(fd, req, arg);
return -errno; } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
}
return ret; drm_dbg(DRM_FUNCTION, "drm_ioctl %x with code %d: %s", req,
} ret, strerror(errno));
static int drm_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, return ret;
unsigned int flags, RK_U32 *handle) }
{
int ret; static void* drm_mmap(int fd, size_t len, int prot, int flags, loff_t offset)
struct drm_mode_create_dumb dmcb; {
memset(&dmcb, 0, sizeof(struct drm_mode_create_dumb)); static unsigned long pagesize_mask = 0;
dmcb.bpp = align;
dmcb.width = ((len + align) & (~align)) / align; if (fd < 0)
dmcb.height = 8; return NULL;
dmcb.size = (len + align) & (~align);
if (!pagesize_mask)
if (handle == NULL) pagesize_mask = getpagesize() - 1;
return -EINVAL;
len = (len + pagesize_mask) & ~pagesize_mask;
ret = drm_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &dmcb);
if (ret < 0) if (offset & 4095) {
return ret; return NULL;
*handle = dmcb.handle; }
(void)heap_mask;
(void)flags; return mmap64(NULL, len, prot, flags, fd, offset);
return ret; }
}
static int drm_handle_to_fd(int fd, RK_U32 handle, int *map_fd, RK_U32 flags)
static int drm_handle_to_fd(int fd, RK_U32 handle, int *map_fd, RK_U32 flags) {
{ int ret;
int ret; struct drm_prime_handle dph;
struct drm_prime_handle dph; memset(&dph, 0, sizeof(struct drm_prime_handle));
memset(&dph, 0, sizeof(struct drm_prime_handle)); dph.handle = handle;
dph.handle = handle; dph.fd = -1;
dph.fd = 1; dph.flags = flags;
dph.flags = 0;
if (map_fd == NULL)
if (map_fd == NULL) return -EINVAL;
return -EINVAL;
ret = drm_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &dph);
ret = drm_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &dph); if (ret < 0) {
if (ret < 0) { return ret;
return ret; }
}
*map_fd = dph.fd;
*map_fd = dph.fd;
drm_dbg(DRM_FUNCTION, "get fd %d", *map_fd);
if (*map_fd < 0) {
mpp_err("map ioctl returned negative fd\n"); if (*map_fd < 0) {
return -EINVAL; mpp_err("map ioctl returned negative fd\n");
} return -EINVAL;
(void)flags; }
return ret;
} return ret;
}
static int drm_free(int fd, RK_U32 handle)
{ static int drm_fd_to_handle(int fd, int map_fd, RK_U32 *handle, RK_U32 flags)
struct drm_mode_destroy_dumb data = { {
.handle = handle, int ret;
}; struct drm_prime_handle dph;
return drm_ioctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
} dph.fd = map_fd;
dph.flags = flags;
static int drm_map(int fd, RK_U32 handle, size_t length, int prot,
int flags, off_t offset, unsigned char **ptr, int *map_fd) ret = drm_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &dph);
{ if (ret < 0) {
int ret; return ret;
struct drm_mode_map_dumb dmmd; }
memset(&dmmd, 0, sizeof(dmmd));
dmmd.handle = handle; *handle = dph.handle;
drm_dbg(DRM_FUNCTION, "get handle %d", *handle);
if (map_fd == NULL)
return -EINVAL; return ret;
if (ptr == NULL) }
return -EINVAL;
static int drm_map(int fd, RK_U32 handle, size_t length, int prot,
ret = drm_handle_to_fd(fd, handle, map_fd, 0); int flags, unsigned char **ptr, int *map_fd)
if (ret < 0) {
return ret; int ret;
struct drm_mode_map_dumb dmmd;
ret = drm_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &dmmd); memset(&dmmd, 0, sizeof(dmmd));
if (ret < 0) dmmd.handle = handle;
return ret;
if (map_fd == NULL)
*ptr = mmap(NULL, length, prot, flags, *map_fd, dmmd.offset); return -EINVAL;
if (*ptr == MAP_FAILED) { if (ptr == NULL)
mpp_err("mmap failed: %s\n", strerror(errno)); return -EINVAL;
return -errno;
} ret = drm_handle_to_fd(fd, handle, map_fd, 0);
(void)offset; drm_dbg(DRM_FUNCTION, "drm_map fd %d", *map_fd);
return ret; if (ret < 0)
} return ret;
#include <dirent.h> ret = drm_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &dmmd);
if (ret < 0) {
static const char *search_name = NULL; close(*map_fd);
return ret;
static int _compare_name(const struct dirent *dir) }
{
if (search_name && strstr(dir->d_name, search_name)) drm_dbg(DRM_FUNCTION, "dev fd %d length %d", fd, length);
return 1;
*ptr = drm_mmap(fd, length, prot, flags, dmmd.offset);
return 0; if (*ptr == MAP_FAILED) {
} close(*map_fd);
*map_fd = -1;
/* mpp_err("mmap failed: %s\n", strerror(errno));
* directory search functdrm: return -errno;
* search directory with dir_name on path. }
* if found match dir append name on path and return
* return ret;
* return 0 for failure }
* return positive value for length of new path
*/ static int drm_alloc(int fd, size_t len, size_t align, RK_U32 *handle)
static RK_S32 find_dir_in_path(char *path, const char *dir_name, size_t max_length) {
{ int ret;
struct dirent **dir; struct drm_mode_create_dumb dmcb;
RK_S32 path_len = strnlen(path, max_length);
RK_S32 new_path_len = 0; drm_dbg(DRM_FUNCTION, "len %ld aligned %ld\n", len, align);
RK_S32 n;
memset(&dmcb, 0, sizeof(struct drm_mode_create_dumb));
search_name = dir_name; dmcb.bpp = 8;
n = scandir(path, &dir, _compare_name, alphasort); dmcb.width = (len + align - 1) & (~(align - 1));
if (n <= 0) { dmcb.height = 1;
mpp_err("scan %s for %s return %d\n", path, dir_name, n); dmcb.size = dmcb.width * dmcb.bpp;
} else {
mpp_assert(n == 1); drm_dbg(DRM_FUNCTION, "fd %d aligned %d size %lld\n", fd, align, dmcb.size);
new_path_len = path_len; if (handle == NULL)
new_path_len += snprintf(path + path_len, max_length - path_len - 1, return -EINVAL;
"/%s", dir[0]->d_name);
free(dir[0]); ret = drm_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &dmcb);
free(dir); if (ret < 0)
} return ret;
search_name = NULL; *handle = dmcb.handle;
return new_path_len;
} drm_dbg(DRM_FUNCTION, "get handle %d size %d", *handle, dmcb.size);
static char *dts_devices[] = { return ret;
"vpu_service", }
"hevc_service",
"rkvdec", static int drm_free(int fd, RK_U32 handle)
"rkvenc", {
}; struct drm_mode_destroy_dumb data = {
.handle = handle,
static RK_S32 check_sysfs_iommu() };
{ return drm_ioctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
RK_U32 i = 0, found = 0; }
RK_S32 ret = DRM_DETECT_IOMMU_DISABLE;
char path[256]; #include <dirent.h>
for (i = 0; i < MPP_ARRAY_ELEMS(dts_devices); i++) { static const char *search_name = NULL;
snprintf(path, sizeof(path), "/proc/device-tree");
if (find_dir_in_path(path, dts_devices[i], sizeof(path))) { static int _compare_name(const struct dirent *dir)
if (find_dir_in_path(path, "iommu_enabled", sizeof(path))) { {
FILE *iommu_fp = fopen(path, "rb"); if (search_name && strstr(dir->d_name, search_name))
return 1;
if (iommu_fp) {
RK_U32 iommu_enabled = 0; return 0;
fread(&iommu_enabled, sizeof(RK_U32), 1, iommu_fp); }
mpp_log("%s iommu_enabled %d\n", dts_devices[i], (iommu_enabled > 0));
fclose(iommu_fp); /*
if (iommu_enabled) * directory search functdrm:
ret = DRM_DETECT_IOMMU_ENABLE; * search directory with dir_name on path.
} * if found match dir append name on path and return
found = 1; *
break; * return 0 for failure
} * return positive value for length of new path
} */
} static RK_S32 find_dir_in_path(char *path, const char *dir_name, size_t max_length)
{
if (!found) { struct dirent **dir;
mpp_err("can not find dts for all possible devices\n"); RK_S32 path_len = strnlen(path, max_length);
ret = DRM_DETECT_NO_DTS; RK_S32 new_path_len = 0;
} RK_S32 n;
return ret; search_name = dir_name;
} n = scandir(path, &dir, _compare_name, alphasort);
if (n <= 0) {
typedef struct { mpp_err("scan %s for %s return %d\n", path, dir_name, n);
RK_U32 alignment; } else {
RK_S32 drm_device; mpp_assert(n == 1);
} allocator_ctx_drm;
new_path_len = path_len;
#define VPU_IOC_MAGIC 'l' new_path_len += snprintf(path + path_len, max_length - path_len - 1,
#define VPU_IOC_PROBE_IOMMU_STATUS _IOR(VPU_IOC_MAGIC, 5, unsigned long) "/%s", dir[0]->d_name);
#define VPU_IOC_PROBE_HEAP_STATUS _IOR(VPU_IOC_MAGIC, 6, unsigned long) free(dir[0]);
free(dir);
enum { }
DRM_HEAP_TYPE_SYSTEM = 0, search_name = NULL;
DRM_HEAP_TYPE_CMA, return new_path_len;
DRM_HEAP_NUMS, }
};
static RK_S32 check_sysfs_iommu()
const char *dev_drm = "/dev/dri/card0"; {
static RK_S32 drm_heap_id = -1; RK_U32 i = 0;
static RK_U32 drm_heap_mask = 1; RK_U32 dts_info_found = 0;
RK_U32 ion_info_found = 0;
MPP_RET os_allocator_drm_open(void **ctx, size_t alignment) RK_S32 ret = DRM_DETECT_IOMMU_DISABLE;
{ char path[256];
RK_S32 fd; static char *dts_devices[] = {
allocator_ctx_drm *p; "vpu_service",
"hevc_service",
if (NULL == ctx) { "rkvdec",
mpp_err("os_allocator_open Android do not accept NULL input\n"); "rkvenc",
return MPP_ERR_NULL_PTR; };
} static char *system_heaps[] = {
"vmalloc",
*ctx = NULL; "system-heap",
};
fd = open(dev_drm, O_RDWR);
if (fd < 0) { mpp_env_get_u32("drm_debug", &drm_debug, 0);
mpp_err("open %s failed!\n", dev_drm); #ifdef SOFIA_3GR_LINUX
return MPP_ERR_UNKNOW; return ret;
} #endif
drm_dbg(DRM_DEVICE, "open drm dev fd %d\n", fd); for (i = 0; i < MPP_ARRAY_ELEMS(dts_devices); i++) {
snprintf(path, sizeof(path), "/proc/device-tree");
p = mpp_malloc(allocator_ctx_drm, 1); if (find_dir_in_path(path, dts_devices[i], sizeof(path))) {
if (NULL == p) { if (find_dir_in_path(path, "iommu_enabled", sizeof(path))) {
close(fd); FILE *iommu_fp = fopen(path, "rb");
mpp_err("os_allocator_open Android failed to allocate context\n");
return MPP_ERR_MALLOC; if (iommu_fp) {
} else { RK_U32 iommu_enabled = 0;
/* fread(&iommu_enabled, sizeof(RK_U32), 1, iommu_fp);
* default drm use cma, do nothing here mpp_log("%s iommu_enabled %d\n", dts_devices[i], (iommu_enabled > 0));
*/ fclose(iommu_fp);
drm_heap_mask = (1 << DRM_HEAP_TYPE_CMA); if (iommu_enabled)
drm_heap_id = DRM_HEAP_TYPE_CMA; ret = DRM_DETECT_IOMMU_ENABLE;
p->alignment = alignment; }
p->drm_device = fd; dts_info_found = 1;
*ctx = p; break;
} }
}
return MPP_OK; }
}
if (!dts_info_found) {
MPP_RET os_allocator_drm_alloc(void *ctx, MppBufferInfo *info) for (i = 0; i < MPP_ARRAY_ELEMS(system_heaps); i++) {
{ snprintf(path, sizeof(path), "/sys/kernel/debug/ion/heaps");
MPP_RET ret = MPP_OK; if (find_dir_in_path(path, system_heaps[i], sizeof(path))) {
allocator_ctx_drm *p = NULL; mpp_log("%s found\n", system_heaps[i]);
ret = DRM_DETECT_IOMMU_ENABLE;
if (NULL == ctx) { ion_info_found = 1;
mpp_err("os_allocator_close Android do not accept NULL input\n"); break;
return MPP_ERR_NULL_PTR; }
} }
}
p = (allocator_ctx_drm *)ctx;
ret = drm_alloc(p->drm_device, info->size, p->alignment, if (!dts_info_found && !ion_info_found) {
drm_heap_mask, 0, mpp_err("can not find any hint from all possible devices\n");
(RK_U32 *)&info->hnd); ret = DRM_DETECT_NO_DTS;
if (ret) { }
mpp_err("os_allocator_drm_alloc drm_alloc failed ret %d\n", ret);
return ret; return ret;
} }
ret = drm_map(p->drm_device, (RK_U32)((intptr_t)info->hnd), info->size,
PROT_READ | PROT_WRITE, MAP_SHARED, (off_t)0, typedef struct {
(unsigned char**)&info->ptr, &info->fd); RK_U32 alignment;
if (ret) { RK_S32 drm_device;
mpp_err("os_allocator_drm_alloc drm_map failed ret %d\n", ret); } allocator_ctx_drm;
return ret;
} #define VPU_IOC_MAGIC 'l'
return ret; #define VPU_IOC_PROBE_IOMMU_STATUS _IOR(VPU_IOC_MAGIC, 5, unsigned long)
} #define VPU_IOC_PROBE_HEAP_STATUS _IOR(VPU_IOC_MAGIC, 6, unsigned long)
MPP_RET os_allocator_drm_import(void *ctx, MppBufferInfo *data) enum {
{ DRM_HEAP_TYPE_SYSTEM = 0,
MPP_RET ret = MPP_OK; DRM_HEAP_TYPE_CMA,
(void)ctx; DRM_HEAP_NUMS,
// NOTE: do not use the original buffer fd, };
// use dup fd to avoid unexpected external fd close
data->fd = dup(data->fd); const char *dev_drm = "/dev/dri/card0";
/* I don't know whether it is correct for drm */
data->ptr = mmap(NULL, data->size, PROT_READ | PROT_WRITE, MAP_SHARED, data->fd, 0); MPP_RET os_allocator_drm_open(void **ctx, size_t alignment)
if (data->ptr == MAP_FAILED) { {
mpp_err_f("map error %s\n", strerror(errno)); RK_S32 fd;
ret = MPP_NOK; allocator_ctx_drm *p;
close(data->fd);
data->fd = -1; drm_dbg(DRM_FUNCTION, "enter");
data->ptr = NULL;
} if (NULL == ctx) {
return ret; mpp_err("os_allocator_open Android do not accept NULL input\n");
} return MPP_ERR_NULL_PTR;
}
MPP_RET os_allocator_drm_release(void *ctx, MppBufferInfo *data)
{ *ctx = NULL;
(void)ctx;
munmap(data->ptr, data->size); fd = open(dev_drm, O_RDWR);
close(data->fd); if (fd < 0) {
return MPP_OK; mpp_err("open %s failed!\n", dev_drm);
} return MPP_ERR_UNKNOW;
}
MPP_RET os_allocator_drm_free(void *ctx, MppBufferInfo *data)
{ drm_dbg(DRM_DEVICE, "open drm dev fd %d\n", fd);
allocator_ctx_drm *p = NULL;
p = mpp_malloc(allocator_ctx_drm, 1);
if (NULL == ctx) { if (NULL == p) {
mpp_err("os_allocator_close Android do not accept NULL input\n"); close(fd);
return MPP_ERR_NULL_PTR; mpp_err("os_allocator_open Android failed to allocate context\n");
} return MPP_ERR_MALLOC;
} else {
p = (allocator_ctx_drm *)ctx; /*
munmap(data->ptr, data->size); * default drm use cma, do nothing here
close(data->fd); */
drm_free(p->drm_device, (RK_U32)((intptr_t)data->hnd)); p->alignment = alignment;
return MPP_OK; p->drm_device = fd;
} *ctx = p;
}
MPP_RET os_allocator_drm_close(void *ctx)
{ drm_dbg(DRM_FUNCTION, "leave");
int ret;
allocator_ctx_drm *p; return MPP_OK;
}
if (NULL == ctx) {
mpp_err("os_allocator_close Android do not accept NULL input\n"); MPP_RET os_allocator_drm_alloc(void *ctx, MppBufferInfo *info)
return MPP_ERR_NULL_PTR; {
} MPP_RET ret = MPP_OK;
allocator_ctx_drm *p = NULL;
p = (allocator_ctx_drm *)ctx;
ret = close(p->drm_device); if (NULL == ctx) {
mpp_free(p); mpp_err("os_allocator_close Android do not accept NULL input\n");
if (ret < 0) return MPP_ERR_NULL_PTR;
return (MPP_RET) - errno; }
return MPP_OK;
} p = (allocator_ctx_drm *)ctx;
drm_dbg(DRM_FUNCTION, "alignment %d size %d", p->alignment, info->size);
os_allocator allocator_drm = { ret = drm_alloc(p->drm_device, info->size, p->alignment,
os_allocator_drm_open, (RK_U32 *)&info->hnd);
os_allocator_drm_alloc, if (ret) {
os_allocator_drm_free, mpp_err("os_allocator_drm_alloc drm_alloc failed ret %d\n", ret);
os_allocator_drm_import, return ret;
os_allocator_drm_release, }
os_allocator_drm_close, drm_dbg(DRM_FUNCTION, "handle %d", (RK_U32)((intptr_t)info->hnd));
}; ret = drm_map(p->drm_device, (RK_U32)((intptr_t)info->hnd), info->size,
PROT_READ | PROT_WRITE, MAP_SHARED, (unsigned char **)&info->ptr, &info->fd);
if (ret) {
mpp_err("os_allocator_drm_alloc drm_map failed ret %d\n", ret);
return ret;
}
return ret;
}
MPP_RET os_allocator_drm_import(void *ctx, MppBufferInfo *data)
{
MPP_RET ret = MPP_OK;
allocator_ctx_drm *p = (allocator_ctx_drm *)ctx;
struct drm_mode_map_dumb dmmd;
memset(&dmmd, 0, sizeof(dmmd));
drm_dbg(DRM_FUNCTION, "enter");
// NOTE: do not use the original buffer fd,
// use dup fd to avoid unexpected external fd close
data->fd = dup(data->fd);
ret = drm_fd_to_handle(p->drm_device, data->fd, (RK_U32 *)&data->hnd, 0);
drm_dbg(DRM_FUNCTION, "get handle %d", (RK_U32)(data->hnd));
dmmd.handle = (RK_U32)(data->hnd);
ret = drm_ioctl(p->drm_device, DRM_IOCTL_MODE_MAP_DUMB, &dmmd);
if (ret < 0)
return ret;
drm_dbg(DRM_FUNCTION, "dev fd %d length %d", p->drm_device, data->size);
data->ptr = drm_mmap(p->drm_device, data->size, PROT_READ | PROT_WRITE, MAP_SHARED, dmmd.offset);
if (data->ptr == MAP_FAILED) {
mpp_err("mmap failed: %s\n", strerror(errno));
return -errno;
}
drm_dbg(DRM_FUNCTION, "leave");
return ret;
}
MPP_RET os_allocator_drm_release(void *ctx, MppBufferInfo *data)
{
(void)ctx;
munmap(data->ptr, data->size);
close(data->fd);
return MPP_OK;
}
MPP_RET os_allocator_drm_free(void *ctx, MppBufferInfo *data)
{
allocator_ctx_drm *p = NULL;
if (NULL == ctx) {
mpp_err("os_allocator_close Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR;
}
p = (allocator_ctx_drm *)ctx;
munmap(data->ptr, data->size);
close(data->fd);
drm_free(p->drm_device, (RK_U32)((intptr_t)data->hnd));
return MPP_OK;
}
MPP_RET os_allocator_drm_close(void *ctx)
{
int ret;
allocator_ctx_drm *p;
if (NULL == ctx) {
mpp_err("os_allocator_close Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR;
}
p = (allocator_ctx_drm *)ctx;
drm_dbg(DRM_FUNCTION, "close fd %d", p->drm_device);
ret = close(p->drm_device);
mpp_free(p);
if (ret < 0)
return (MPP_RET) - errno;
return MPP_OK;
}
os_allocator allocator_drm = {
os_allocator_drm_open,
os_allocator_drm_alloc,
os_allocator_drm_free,
os_allocator_drm_import,
os_allocator_drm_release,
os_allocator_drm_close,
};

View File

@@ -1,138 +1,148 @@
/* /*
* Copyright 2015 Rockchip Electronics Co. LTD * Copyright 2015 Rockchip Electronics Co. LTD
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include "os_mem.h" #include "os_mem.h"
#include "os_allocator.h" #include "os_allocator.h"
#include "allocator_ion.h" #include "allocator_ion.h"
#include "allocator_drm.h"
#include "mpp_mem.h"
#include "mpp_log.h" #include "mpp_mem.h"
#include "mpp_log.h"
typedef struct { #include "mpp_common.h"
RK_U32 alignment;
RK_S32 fd_count; typedef struct {
} allocator_ctx_normal; RK_U32 alignment;
RK_S32 fd_count;
MPP_RET os_allocator_normal_open(void **ctx, size_t alignment) } allocator_ctx_normal;
{
MPP_RET ret = MPP_OK; MPP_RET os_allocator_normal_open(void **ctx, size_t alignment)
allocator_ctx_normal *p = NULL; {
MPP_RET ret = MPP_OK;
if (NULL == ctx) { allocator_ctx_normal *p = NULL;
mpp_err("os_allocator_open Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR; if (NULL == ctx) {
} mpp_err("os_allocator_open Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR;
p = mpp_malloc(allocator_ctx_normal, 1); }
if (NULL == p) {
mpp_err("os_allocator_open Android failed to allocate context\n"); p = mpp_malloc(allocator_ctx_normal, 1);
ret = MPP_ERR_MALLOC; if (NULL == p) {
} else mpp_err("os_allocator_open Android failed to allocate context\n");
p->alignment = alignment; ret = MPP_ERR_MALLOC;
} else
p->fd_count = 0; p->alignment = alignment;
*ctx = p; p->fd_count = 0;
return ret;
} *ctx = p;
return ret;
MPP_RET os_allocator_normal_alloc(void *ctx, MppBufferInfo *info) }
{
allocator_ctx_normal *p = NULL; MPP_RET os_allocator_normal_alloc(void *ctx, MppBufferInfo *info)
{
if (NULL == ctx) { allocator_ctx_normal *p = NULL;
mpp_err("os_allocator_close Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR; if (NULL == ctx) {
} mpp_err("os_allocator_close Android do not accept NULL input\n");
return MPP_ERR_NULL_PTR;
p = (allocator_ctx_normal *)ctx; }
info->fd = p->fd_count++;
return os_malloc(&info->ptr, p->alignment, info->size); p = (allocator_ctx_normal *)ctx;
} info->fd = p->fd_count++;
return os_malloc(&info->ptr, p->alignment, info->size);
MPP_RET os_allocator_normal_free(void *ctx, MppBufferInfo *info) }
{
(void) ctx; MPP_RET os_allocator_normal_free(void *ctx, MppBufferInfo *info)
if (info->ptr) {
os_free(info->ptr); (void)ctx;
return MPP_OK; if (info->ptr)
} os_free(info->ptr);
return MPP_OK;
MPP_RET os_allocator_normal_import(void *ctx, MppBufferInfo *info) }
{
allocator_ctx_normal *p = (allocator_ctx_normal *)ctx; MPP_RET os_allocator_normal_import(void *ctx, MppBufferInfo *info)
mpp_assert(ctx); {
mpp_assert(info->ptr); allocator_ctx_normal *p = (allocator_ctx_normal *)ctx;
mpp_assert(info->size); mpp_assert(ctx);
info->hnd = NULL; mpp_assert(info->ptr);
info->fd = p->fd_count++; mpp_assert(info->size);
return MPP_OK; info->hnd = NULL;
} info->fd = p->fd_count++;
return MPP_OK;
MPP_RET os_allocator_normal_release(void *ctx, MppBufferInfo *info) }
{
(void) ctx; MPP_RET os_allocator_normal_release(void *ctx, MppBufferInfo *info)
mpp_assert(info->ptr); {
mpp_assert(info->size); (void)ctx;
info->ptr = NULL; mpp_assert(info->ptr);
info->size = 0; mpp_assert(info->size);
info->hnd = NULL; info->ptr = NULL;
info->fd = -1; info->size = 0;
return MPP_OK; info->hnd = NULL;
} info->fd = -1;
return MPP_OK;
MPP_RET os_allocator_normal_close(void *ctx) }
{
if (ctx) { MPP_RET os_allocator_normal_close(void *ctx)
mpp_free(ctx); {
return MPP_OK; if (ctx) {
} mpp_free(ctx);
mpp_err("os_allocator_close Linux found NULL context input\n"); return MPP_OK;
return MPP_NOK; }
} mpp_err("os_allocator_close Linux found NULL context input\n");
return MPP_NOK;
static os_allocator allocator_normal = { }
os_allocator_normal_open,
os_allocator_normal_alloc, static os_allocator allocator_normal = {
os_allocator_normal_free, os_allocator_normal_open,
os_allocator_normal_import, os_allocator_normal_alloc,
os_allocator_normal_release, os_allocator_normal_free,
os_allocator_normal_close, os_allocator_normal_import,
}; os_allocator_normal_release,
os_allocator_normal_close,
MPP_RET os_allocator_get(os_allocator *api, MppBufferType type) };
{
MPP_RET ret = MPP_OK; MPP_RET os_allocator_get(os_allocator *api, MppBufferType type)
switch (type) { {
case MPP_BUFFER_TYPE_NORMAL : MPP_RET ret = MPP_OK;
case MPP_BUFFER_TYPE_V4L2 : {
*api = allocator_normal; switch (type) {
} break; case MPP_BUFFER_TYPE_NORMAL :
case MPP_BUFFER_TYPE_ION : { case MPP_BUFFER_TYPE_V4L2 : {
*api = allocator_ion; *api = allocator_normal;
} break; }
default : { break;
ret = MPP_NOK; case MPP_BUFFER_TYPE_ION : {
} break; *api = allocator_ion;
} }
return ret; break;
} case MPP_BUFFER_TYPE_DRM : {
*api = allocator_drm;
}
break;
default : {
ret = MPP_NOK;
}
break;
}
return ret;
}

View File

@@ -630,17 +630,17 @@ struct drm_get_cap {
/** /**
* DRM_CLIENT_CAP_UNIVERSAL_PLANES * DRM_CLIENT_CAP_UNIVERSAL_PLANES
* *
* if set to 1, the DRM core will expose the full universal plane list * If set to 1, the DRM core will expose all planes (overlay, primary, and
* (including primary and cursor planes). * cursor) to userspace.
*/ */
#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
/** /**
* DRM_CLIENT_CAP_ATOMIC * DRM_CLIENT_CAP_ATOMIC
* *
* If set to 1, the DRM core will allow atomic modesetting requests. * If set to 1, the DRM core will expose atomic properties to userspace
*/ */
#define DRM_CLIENT_CAP_ATOMIC 3 #define DRM_CLIENT_CAP_ATOMIC 3
/** DRM_IOCTL_SET_CLIENT_CAP ioctl argument type */ /** DRM_IOCTL_SET_CLIENT_CAP ioctl argument type */
struct drm_set_client_cap { struct drm_set_client_cap {
@@ -816,7 +816,6 @@ struct drm_event_vblank {
#define DRM_CAP_PRIME 0x5 #define DRM_CAP_PRIME 0x5
#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
#define DRM_CAP_ASYNC_PAGE_FLIP 0x7 #define DRM_CAP_ASYNC_PAGE_FLIP 0x7
#define DRM_CAP_ADDFB2_MODIFIERS 0x10
#define DRM_PRIME_CAP_IMPORT 0x1 #define DRM_PRIME_CAP_IMPORT 0x1
#define DRM_PRIME_CAP_EXPORT 0x2 #define DRM_PRIME_CAP_EXPORT 0x2

View File

@@ -92,6 +92,14 @@
#define DRM_MODE_DIRTY_ON 1 #define DRM_MODE_DIRTY_ON 1
#define DRM_MODE_DIRTY_ANNOTATE 2 #define DRM_MODE_DIRTY_ANNOTATE 2
/* rotation property bits */
#define DRM_ROTATE_0 0
#define DRM_ROTATE_90 1
#define DRM_ROTATE_180 2
#define DRM_ROTATE_270 3
#define DRM_REFLECT_X 4
#define DRM_REFLECT_Y 5
struct drm_mode_modeinfo { struct drm_mode_modeinfo {
__u32 clock; __u32 clock;
__u16 hdisplay, hsync_start, hsync_end, htotal, hskew; __u16 hdisplay, hsync_start, hsync_end, htotal, hskew;
@@ -259,6 +267,13 @@ struct drm_mode_get_connector {
#define DRM_MODE_PROP_OBJECT DRM_MODE_PROP_TYPE(1) #define DRM_MODE_PROP_OBJECT DRM_MODE_PROP_TYPE(1)
#define DRM_MODE_PROP_SIGNED_RANGE DRM_MODE_PROP_TYPE(2) #define DRM_MODE_PROP_SIGNED_RANGE DRM_MODE_PROP_TYPE(2)
/* the PROP_ATOMIC flag is used to hide properties from userspace that
* is not aware of atomic properties. This is mostly to work around
* older userspace (DDX drivers) that read/write each prop they find,
* witout being aware that this could be triggering a lengthy modeset.
*/
#define DRM_MODE_PROP_ATOMIC 0x80000000
struct drm_mode_property_enum { struct drm_mode_property_enum {
__u64 value; __u64 value;
char name[DRM_PROP_NAME_LEN]; char name[DRM_PROP_NAME_LEN];
@@ -322,8 +337,7 @@ struct drm_mode_fb_cmd {
__u32 handle; __u32 handle;
}; };
#define DRM_MODE_FB_INTERLACED (1<<0) /* for interlaced framebuffers */ #define DRM_MODE_FB_INTERLACED (1<<0) /* for interlaced framebuffers */
#define DRM_MODE_FB_MODIFIERS (1<<1) /* enables ->modifer[] */
struct drm_mode_fb_cmd2 { struct drm_mode_fb_cmd2 {
__u32 fb_id; __u32 fb_id;
@@ -344,18 +358,10 @@ struct drm_mode_fb_cmd2 {
* So it would consist of Y as offset[0] and UV as * So it would consist of Y as offset[0] and UV as
* offset[1]. Note that offset[0] will generally * offset[1]. Note that offset[0] will generally
* be 0. * be 0.
*
* To accommodate tiled, compressed, etc formats, a per-plane
* modifier can be specified. The default value of zero
* indicates "native" format as specified by the fourcc.
* Vendor specific modifier token. This allows, for example,
* different tiling/swizzling pattern on different planes.
* See discussion above of DRM_FORMAT_MOD_xxx.
*/ */
__u32 handles[4]; __u32 handles[4];
__u32 pitches[4]; /* pitch for each plane */ __u32 pitches[4]; /* pitch for each plane */
__u32 offsets[4]; /* offset of each plane */ __u32 offsets[4]; /* offset of each plane */
__u64 modifier[4]; /* ie, tiling, compressed (per plane) */
}; };
#define DRM_MODE_FB_DIRTY_ANNOTATE_COPY 0x01 #define DRM_MODE_FB_DIRTY_ANNOTATE_COPY 0x01
@@ -517,9 +523,16 @@ struct drm_mode_destroy_dumb {
}; };
/* page-flip flags are valid, plus: */ /* page-flip flags are valid, plus: */
#define DRM_MODE_ATOMIC_TEST_ONLY 0x0100 #define DRM_MODE_ATOMIC_TEST_ONLY 0x0100
#define DRM_MODE_ATOMIC_NONBLOCK 0x0200 #define DRM_MODE_ATOMIC_NONBLOCK 0x0200
#define DRM_MODE_ATOMIC_ALLOW_MODESET 0x0400 #define DRM_MODE_ATOMIC_ALLOW_MODESET 0x0400
#define DRM_MODE_ATOMIC_FLAGS (\
DRM_MODE_PAGE_FLIP_EVENT |\
DRM_MODE_PAGE_FLIP_ASYNC |\
DRM_MODE_ATOMIC_TEST_ONLY |\
DRM_MODE_ATOMIC_NONBLOCK |\
DRM_MODE_ATOMIC_ALLOW_MODESET)
struct drm_mode_atomic { struct drm_mode_atomic {
__u32 flags; __u32 flags;
@@ -552,5 +565,4 @@ struct drm_mode_destroy_blob {
__u32 blob_id; __u32 blob_id;
}; };
#endif #endif

File diff suppressed because it is too large Load Diff