[alloctor]: add flags for alloctor

Each of the different alloctor has different alloctor modes,
such as allocating physical continuous memory, secure memory,
etc.

Change-Id: Ib032689f0cfd36b494843dcc176a22c6c9587b6a
Signed-off-by: Rimon Xu <rimon.xu@rock-chips.com>
This commit is contained in:
Rimon Xu
2018-05-04 11:40:21 +08:00
committed by Herman Chen
parent 4863d54ff3
commit fb73f23f53
13 changed files with 82 additions and 24 deletions

View File

@@ -155,6 +155,27 @@ typedef enum {
MPP_BUFFER_TYPE_BUTT,
} MppBufferType;
#define MPP_BUFFER_TYPE_MASK 0x0000FFFF
/*
* MPP_BUFFER_FLAGS cooperate with MppBufferType
* 16 high bits of MppBufferType are used in flags
*
* eg:
* DRM CMA buffer : MPP_BUFFER_TYPE_DRM | MPP_BUFFER_FLAGS_CONTIG
* = 0x00010003
* DRM SECURE buffer: MPP_BUFFER_TYPE_DRM | MPP_BUFFER_FLAGS_SECURE
* = 0x00080003
*
* flags originate from drm_rockchip_gem_mem_type
*/
#define MPP_BUFFER_FLAGS_MASK 0x000f0000 //ROCKCHIP_BO_MASK << 16
#define MPP_BUFFER_FLAGS_CONTIG 0x00010000 //ROCKCHIP_BO_CONTIG << 16
#define MPP_BUFFER_FLAGS_CACHABLE 0x00020000 //ROCKCHIP_BO_CACHABLE << 16
#define MPP_BUFFER_FLAGS_WC 0x00040000 //ROCKCHIP_BO_WC << 16
#define MPP_BUFFER_FLAGS_SECURE 0x00080000 //ROCKCHIP_BO_SECURE << 16
/*
* MppBufferInfo variable's meaning is different in different MppBufferType
*

View File

@@ -34,7 +34,8 @@ MPP_RET mpp_buffer_import_with_tag(MppBufferGroup group, MppBufferInfo *info, Mp
if (p) {
// if group is specified we need to check the parameter
if (p->type != info->type || p->type >= MPP_BUFFER_TYPE_BUTT ||
if ((p->type & MPP_BUFFER_TYPE_MASK) != info->type ||
(p->type & MPP_BUFFER_TYPE_MASK) >= MPP_BUFFER_TYPE_BUTT ||
p->mode != MPP_BUFFER_EXTERNAL) {
mpp_err("mpp_buffer_commit invalid type found group %d info %d group mode %d\n",
p->type, info->type, p->mode);
@@ -259,7 +260,7 @@ MPP_RET mpp_buffer_group_get(MppBufferGroup *group, MppBufferType type, MppBuffe
{
if (NULL == group ||
mode >= MPP_BUFFER_MODE_BUTT ||
type >= MPP_BUFFER_TYPE_BUTT) {
(type & MPP_BUFFER_TYPE_MASK) >= MPP_BUFFER_TYPE_BUTT) {
mpp_err_f("input invalid group %p mode %d type %d\n",
group, mode, type);
return MPP_ERR_UNKNOW;

View File

@@ -644,6 +644,7 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal
MppBufferMode mode, MppBufferType type,
RK_U32 is_misc)
{
MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
MppBufferGroupImpl *p = mpp_calloc(MppBufferGroupImpl, 1);
if (NULL == p) {
mpp_err("MppBufferService failed to allocate group context\n");
@@ -670,7 +671,7 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal
}
p->caller = caller;
p->mode = mode;
p->type = type;
p->type = buffer_type;
p->limit = BUFFER_GROUP_SIZE_DEFAULT;
p->group_id = id;
p->clear_on_exit = (mpp_buffer_debug & MPP_BUF_DBG_CLR_ON_EXIT) ? (1) : (0);
@@ -680,10 +681,10 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal
buffer_group_add_log(p, NULL, GRP_CREATE, __FUNCTION__);
mpp_assert(mode < MPP_BUFFER_MODE_BUTT);
mpp_assert(type < MPP_BUFFER_TYPE_BUTT);
mpp_assert(buffer_type < MPP_BUFFER_TYPE_BUTT);
if (is_misc) {
misc[mode][type] = p;
misc[mode][buffer_type] = p;
misc_count++;
}
@@ -692,6 +693,7 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal
MppBufferGroupImpl *MppBufferService::get_misc(MppBufferMode mode, MppBufferType type)
{
type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
if (type == MPP_BUFFER_TYPE_NORMAL)
return NULL;
@@ -703,6 +705,7 @@ MppBufferGroupImpl *MppBufferService::get_misc(MppBufferMode mode, MppBufferType
void MppBufferService::set_misc(MppBufferMode mode, MppBufferType type, MppBufferGroupImpl *val)
{
type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
if (type == MPP_BUFFER_TYPE_NORMAL)
return ;

View File

@@ -78,6 +78,7 @@ static inline void *drm_mmap(void *addr, size_t length, int prot, int flags,
typedef struct {
RK_U32 alignment;
RK_S32 drm_device;
RK_U32 flags;
} allocator_ctx_drm;
static const char *dev_drm = "/dev/dri/card0";
@@ -144,7 +145,7 @@ static int drm_fd_to_handle(int fd, int map_fd, RK_U32 *handle, RK_U32 flags)
return ret;
}
static int drm_alloc(int fd, size_t len, size_t align, RK_U32 *handle)
static int drm_alloc(int fd, size_t len, size_t align, RK_U32 *handle, RK_U32 flags)
{
int ret;
struct drm_mode_create_dumb dmcb;
@@ -155,6 +156,7 @@ static int drm_alloc(int fd, size_t len, size_t align, RK_U32 *handle)
dmcb.bpp = 8;
dmcb.width = (len + align - 1) & (~(align - 1));
dmcb.height = 1;
dmcb.flags = flags;
if (handle == NULL)
return -EINVAL;
@@ -179,7 +181,7 @@ static int drm_free(int fd, RK_U32 handle)
return drm_ioctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
}
static MPP_RET os_allocator_drm_open(void **ctx, size_t alignment)
static MPP_RET os_allocator_drm_open(void **ctx, MppAllocatorCfg *cfg)
{
RK_S32 fd;
allocator_ctx_drm *p;
@@ -212,7 +214,8 @@ static MPP_RET os_allocator_drm_open(void **ctx, size_t alignment)
/*
* default drm use cma, do nothing here
*/
p->alignment = alignment;
p->alignment = cfg->alignment;
p->flags = cfg->flags;
p->drm_device = fd;
*ctx = p;
}
@@ -235,7 +238,7 @@ static MPP_RET os_allocator_drm_alloc(void *ctx, MppBufferInfo *info)
p = (allocator_ctx_drm *)ctx;
drm_dbg(DRM_FUNCTION, "alignment %d size %d", p->alignment, info->size);
ret = drm_alloc(p->drm_device, info->size, p->alignment,
(RK_U32 *)&info->hnd);
(RK_U32 *)&info->hnd, p->flags);
if (ret) {
mpp_err("os_allocator_drm_alloc drm_alloc failed ret %d\n", ret);
return ret;

View File

@@ -27,7 +27,7 @@ typedef struct {
size_t alignment;
} allocator_ctx;
static MPP_RET allocator_ext_dma_open(void **ctx, size_t alignment)
static MPP_RET allocator_ext_dma_open(void **ctx, MppAllocatorCfg *cfg)
{
MPP_RET ret = MPP_OK;
allocator_ctx *p = NULL;
@@ -42,7 +42,7 @@ static MPP_RET allocator_ext_dma_open(void **ctx, size_t alignment)
mpp_err_f("failed to allocate context\n");
ret = MPP_ERR_MALLOC;
} else {
p->alignment = alignment;
p->alignment = cfg->alignment;
}
*ctx = p;

View File

@@ -267,7 +267,7 @@ static RK_S32 ion_heap_id = -1;
static RK_U32 ion_heap_mask = ION_HEAP_SYSTEM_MASK;
static pthread_mutex_t lock;
static MPP_RET allocator_ion_open(void **ctx, size_t alignment)
static MPP_RET allocator_ion_open(void **ctx, MppAllocatorCfg *cfg)
{
RK_S32 fd;
allocator_ctx_ion *p;
@@ -329,7 +329,7 @@ static MPP_RET allocator_ion_open(void **ctx, size_t alignment)
mpp_log("using ion heap %s\n", heap_name);
}
pthread_mutex_unlock(&lock);
p->alignment = alignment;
p->alignment = cfg->alignment;
p->ion_device = fd;
*ctx = p;
}

View File

@@ -27,7 +27,7 @@ typedef struct {
RK_S32 fd_count;
} allocator_ctx;
static MPP_RET allocator_std_open(void **ctx, size_t alignment)
static MPP_RET allocator_std_open(void **ctx, MppAllocatorCfg *cfg)
{
MPP_RET ret = MPP_OK;
allocator_ctx *p = NULL;
@@ -42,7 +42,7 @@ static MPP_RET allocator_std_open(void **ctx, size_t alignment)
mpp_err_f("failed to allocate context\n");
ret = MPP_ERR_MALLOC;
} else
p->alignment = alignment;
p->alignment = cfg->alignment;
p->fd_count = 0;

View File

@@ -22,6 +22,12 @@
typedef void *MppAllocator;
typedef struct MppAllocatorCfg_t {
// input
size_t alignment;
RK_U32 flags;
} MppAllocatorCfg;
typedef struct MppAllocatorApi_t {
RK_U32 size;
RK_U32 version;

View File

@@ -73,6 +73,17 @@ typedef unsigned int drm_context_t;
typedef unsigned int drm_drawable_t;
typedef unsigned int drm_magic_t;
enum drm_rockchip_gem_mem_type {
/* Physically Continuous memory and used as default. */
ROCKCHIP_BO_CONTIG = 1 << 0,
/* cachable mapping. */
ROCKCHIP_BO_CACHABLE = 1 << 1,
/* write-combine mapping. */
ROCKCHIP_BO_WC = 1 << 2,
ROCKCHIP_BO_SECURE = 1 << 3,
ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE | ROCKCHIP_BO_WC
};
/**
* Cliprect.
*

View File

@@ -23,6 +23,8 @@
#include "os_allocator.h"
#include <linux/drm.h>
#define MPP_ALLOCATOR_LOCK(p) pthread_mutex_lock(&(p)->lock);
#define MPP_ALLOCATOR_UNLOCK(p) pthread_mutex_unlock(&(p)->lock);
@@ -118,9 +120,12 @@ static MppAllocatorApi mpp_allocator_api = {
MPP_RET mpp_allocator_get(MppAllocator *allocator,
MppAllocatorApi **api, MppBufferType type)
{
if (NULL == allocator || NULL == api || type >= MPP_BUFFER_TYPE_BUTT) {
MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
RK_U32 flags = (type & MPP_BUFFER_FLAGS_MASK) >> 16;
if (NULL == allocator || NULL == api || buffer_type >= MPP_BUFFER_TYPE_BUTT) {
mpp_err_f("invalid input: allocator %p api %p type %d\n",
allocator, api, type);
allocator, api, buffer_type);
return MPP_ERR_UNKNOW;
}
@@ -128,14 +133,20 @@ MPP_RET mpp_allocator_get(MppAllocator *allocator,
if (NULL == p) {
mpp_err("mpp_allocator_get failed to malloc allocator context\n");
return MPP_ERR_NULL_PTR;
} else
p->type = type;
} else {
p->type = buffer_type;
p->flags = flags;
}
MPP_RET ret = os_allocator_get(&p->os_api, type);
MPP_RET ret = os_allocator_get(&p->os_api, buffer_type);
if (MPP_OK == ret) {
p->alignment = SZ_4K;
ret = p->os_api.open(&p->ctx, p->alignment);
MppAllocatorCfg cfg = {
.alignment = SZ_4K,
.flags = flags,
};
ret = p->os_api.open(&p->ctx, &cfg);
}
if (MPP_OK == ret) {
pthread_mutexattr_t attr;

View File

@@ -23,6 +23,7 @@
typedef struct MppAllocatorImpl_t {
pthread_mutex_t lock;
MppBufferType type;
RK_U32 flags;
size_t alignment;
os_allocator os_api;
void *ctx;

View File

@@ -70,7 +70,8 @@ public:
RK_U32 MppRuntimeService::get_allocator_valid(MppBufferType type)
{
return (type < MPP_BUFFER_TYPE_BUTT) ? allocator_valid[type] : (0);
MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
return (buffer_type < MPP_BUFFER_TYPE_BUTT) ? allocator_valid[buffer_type] : (0);
};
MppRuntimeService::MppRuntimeService()

View File

@@ -22,7 +22,7 @@
typedef MPP_RET (*OsAllocatorFunc)(void *ctx, MppBufferInfo *info);
typedef struct os_allocator_t {
MPP_RET (*open)(void **ctx, size_t alignment);
MPP_RET (*open)(void **ctx, MppAllocatorCfg *cfg);
MPP_RET (*close)(void *ctx);
OsAllocatorFunc alloc;