mirror of
https://github.com/nyanmisaka/mpp.git
synced 2025-10-05 01:02:39 +08:00
[allocator]: add import and release function to os/mpp allocator
git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@339 6e48237b-75ef-9749-8fc9-41990f28c85a
This commit is contained in:
@@ -73,6 +73,8 @@ MPP_RET deinit_buffer_no_lock(MppBufferImpl *buffer)
|
|||||||
if (group) {
|
if (group) {
|
||||||
if (buffer->internal)
|
if (buffer->internal)
|
||||||
group->alloc_api->free(group->allocator, &buffer->info);
|
group->alloc_api->free(group->allocator, &buffer->info);
|
||||||
|
else
|
||||||
|
group->alloc_api->release(group->allocator, &buffer->info);
|
||||||
|
|
||||||
group->usage -= buffer->info.size;
|
group->usage -= buffer->info.size;
|
||||||
group->count--;
|
group->count--;
|
||||||
@@ -138,7 +140,7 @@ MPP_RET mpp_buffer_create(const char *tag, RK_U32 group_id, MppBufferInfo *info)
|
|||||||
|
|
||||||
MppBufferGroupImpl *group = SEARCH_GROUP_NORMAL(group_id);
|
MppBufferGroupImpl *group = SEARCH_GROUP_NORMAL(group_id);
|
||||||
if (group) {
|
if (group) {
|
||||||
if (NULL == info->ptr && info->fd == -1) {
|
if (info->fd == -1) {
|
||||||
MPP_RET ret = group->alloc_api->alloc(group->allocator, info);
|
MPP_RET ret = group->alloc_api->alloc(group->allocator, info);
|
||||||
if (MPP_OK != ret) {
|
if (MPP_OK != ret) {
|
||||||
mpp_err("mpp_buffer_create failed to create buffer with size %d\n", info->size);
|
mpp_err("mpp_buffer_create failed to create buffer with size %d\n", info->size);
|
||||||
@@ -146,6 +148,13 @@ MPP_RET mpp_buffer_create(const char *tag, RK_U32 group_id, MppBufferInfo *info)
|
|||||||
return MPP_ERR_MALLOC;
|
return MPP_ERR_MALLOC;
|
||||||
}
|
}
|
||||||
p->internal = 1;
|
p->internal = 1;
|
||||||
|
} else {
|
||||||
|
MPP_RET ret = group->alloc_api->import(group->allocator, info);
|
||||||
|
if (MPP_OK != ret) {
|
||||||
|
mpp_err("mpp_buffer_create failed to create buffer with size %d\n", info->size);
|
||||||
|
mpp_free(p);
|
||||||
|
return MPP_ERR_MALLOC;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p->info = *info;
|
p->info = *info;
|
||||||
@@ -220,7 +229,7 @@ MppBufferImpl *mpp_buffer_get_unused(MppBufferGroupImpl *p, size_t size)
|
|||||||
if (!list_empty(&p->list_unused)) {
|
if (!list_empty(&p->list_unused)) {
|
||||||
MppBufferImpl *pos, *n;
|
MppBufferImpl *pos, *n;
|
||||||
list_for_each_entry_safe(pos, n, &p->list_unused, MppBufferImpl, list_status) {
|
list_for_each_entry_safe(pos, n, &p->list_unused, MppBufferImpl, list_status) {
|
||||||
if (pos->info.size == size) {
|
if (pos->info.size >= size) {
|
||||||
buffer = pos;
|
buffer = pos;
|
||||||
inc_buffer_ref_no_lock(buffer);
|
inc_buffer_ref_no_lock(buffer);
|
||||||
break;
|
break;
|
||||||
@@ -297,7 +306,8 @@ MPP_RET mpp_buffer_group_deinit(MppBufferGroupImpl *p)
|
|||||||
// if any buffer with mode MPP_BUFFER_MODE_COMMIT found it should be error
|
// if any buffer with mode MPP_BUFFER_MODE_COMMIT found it should be error
|
||||||
MppBufferImpl *pos, *n;
|
MppBufferImpl *pos, *n;
|
||||||
list_for_each_entry_safe(pos, n, &p->list_used, MppBufferImpl, list_status) {
|
list_for_each_entry_safe(pos, n, &p->list_used, MppBufferImpl, list_status) {
|
||||||
mpp_assert(pos->mode != MPP_BUFFER_MODE_LIMIT);
|
// mpp_assert(pos->mode != MPP_BUFFER_MODE_LIMIT);
|
||||||
|
mpp_err_f("buffer %p is not free\n", pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -87,6 +87,8 @@ static os_allocator allocator_normal = {
|
|||||||
os_allocator_normal_open,
|
os_allocator_normal_open,
|
||||||
os_allocator_normal_alloc,
|
os_allocator_normal_alloc,
|
||||||
os_allocator_normal_free,
|
os_allocator_normal_free,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
os_allocator_normal_close,
|
os_allocator_normal_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -237,6 +239,20 @@ MPP_RET os_allocator_ion_alloc(void *ctx, MppBufferInfo *info)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MPP_RET os_allocator_ion_import(void *ctx, MppBufferInfo *data)
|
||||||
|
{
|
||||||
|
(void)ctx;
|
||||||
|
data->ptr = mmap(NULL, data->size, PROT_READ | PROT_WRITE, MAP_SHARED, data->fd, 0);
|
||||||
|
return (data->ptr) ? (MPP_OK) : (MPP_NOK);
|
||||||
|
}
|
||||||
|
|
||||||
|
MPP_RET os_allocator_ion_release(void *ctx, MppBufferInfo *data)
|
||||||
|
{
|
||||||
|
(void)ctx;
|
||||||
|
munmap(data->ptr, data->size);
|
||||||
|
return MPP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
MPP_RET os_allocator_ion_free(void *ctx, MppBufferInfo *data)
|
MPP_RET os_allocator_ion_free(void *ctx, MppBufferInfo *data)
|
||||||
{
|
{
|
||||||
allocator_ctx_ion *p = NULL;
|
allocator_ctx_ion *p = NULL;
|
||||||
@@ -273,6 +289,8 @@ static os_allocator allocator_ion = {
|
|||||||
os_allocator_ion_open,
|
os_allocator_ion_open,
|
||||||
os_allocator_ion_alloc,
|
os_allocator_ion_alloc,
|
||||||
os_allocator_ion_free,
|
os_allocator_ion_free,
|
||||||
|
os_allocator_ion_import,
|
||||||
|
os_allocator_ion_release,
|
||||||
os_allocator_ion_close,
|
os_allocator_ion_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -28,6 +28,8 @@ typedef struct MppAllocatorApi_t {
|
|||||||
|
|
||||||
MPP_RET (*alloc)(MppAllocator allocator, MppBufferInfo *data);
|
MPP_RET (*alloc)(MppAllocator allocator, MppBufferInfo *data);
|
||||||
MPP_RET (*free)(MppAllocator allocator, MppBufferInfo *data);
|
MPP_RET (*free)(MppAllocator allocator, MppBufferInfo *data);
|
||||||
|
MPP_RET (*import)(MppAllocator allocator, MppBufferInfo *data);
|
||||||
|
MPP_RET (*release)(MppAllocator allocator, MppBufferInfo *data);
|
||||||
} MppAllocatorApi;
|
} MppAllocatorApi;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@@ -86,6 +86,8 @@ static os_allocator allocator_normal = {
|
|||||||
os_allocator_normal_open,
|
os_allocator_normal_open,
|
||||||
os_allocator_normal_alloc,
|
os_allocator_normal_alloc,
|
||||||
os_allocator_normal_free,
|
os_allocator_normal_free,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
os_allocator_normal_close,
|
os_allocator_normal_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -93,6 +95,8 @@ static os_allocator allocator_v4l2 = {
|
|||||||
os_allocator_normal_open,
|
os_allocator_normal_open,
|
||||||
os_allocator_normal_alloc,
|
os_allocator_normal_alloc,
|
||||||
os_allocator_normal_free,
|
os_allocator_normal_free,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
os_allocator_normal_close,
|
os_allocator_normal_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -29,7 +29,7 @@
|
|||||||
MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info)
|
MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info)
|
||||||
{
|
{
|
||||||
if (NULL == allocator || NULL == info) {
|
if (NULL == allocator || NULL == info) {
|
||||||
mpp_err("mpp_allocator_alloc invalid input: allocator %p info %p\n",
|
mpp_err_f("invalid input: allocator %p info %p\n",
|
||||||
allocator, info);
|
allocator, info);
|
||||||
return MPP_ERR_UNKNOW;
|
return MPP_ERR_UNKNOW;
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info)
|
|||||||
MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info)
|
MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info)
|
||||||
{
|
{
|
||||||
if (NULL == allocator || NULL == info) {
|
if (NULL == allocator || NULL == info) {
|
||||||
mpp_err("mpp_allocator_alloc invalid input: allocator %p info %p\n",
|
mpp_err_f("invalid input: allocator %p info %p\n",
|
||||||
allocator, info);
|
allocator, info);
|
||||||
return MPP_ERR_UNKNOW;
|
return MPP_ERR_UNKNOW;
|
||||||
}
|
}
|
||||||
@@ -62,11 +62,51 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MPP_RET mpp_allocator_import(MppAllocator allocator, MppBufferInfo *info)
|
||||||
|
{
|
||||||
|
if (NULL == info) {
|
||||||
|
mpp_err_f("invalid input: info %p\n", info);
|
||||||
|
return MPP_ERR_UNKNOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
MPP_RET ret = MPP_OK;
|
||||||
|
MppAllocatorImpl *p = (MppAllocatorImpl *)allocator;
|
||||||
|
if (p->os_api.import) {
|
||||||
|
MPP_ALLOCATOR_LOCK(p);
|
||||||
|
ret = p->os_api.import(p->ctx, info);
|
||||||
|
MPP_ALLOCATOR_UNLOCK(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MPP_RET mpp_allocator_release(MppAllocator allocator, MppBufferInfo *info)
|
||||||
|
{
|
||||||
|
if (NULL == allocator || NULL == info) {
|
||||||
|
mpp_err("mpp_allocator_alloc invalid input: allocator %p info %p\n",
|
||||||
|
allocator, info);
|
||||||
|
return MPP_ERR_UNKNOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
MPP_RET ret = MPP_OK;
|
||||||
|
MppAllocatorImpl *p = (MppAllocatorImpl *)allocator;
|
||||||
|
if (p->os_api.release) {
|
||||||
|
MPP_ALLOCATOR_LOCK(p);
|
||||||
|
ret = p->os_api.release(p->ctx, info);
|
||||||
|
MPP_ALLOCATOR_UNLOCK(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static MppAllocatorApi mpp_allocator_api = {
|
static MppAllocatorApi mpp_allocator_api = {
|
||||||
sizeof(mpp_allocator_api),
|
sizeof(mpp_allocator_api),
|
||||||
1,
|
1,
|
||||||
mpp_allocator_alloc,
|
mpp_allocator_alloc,
|
||||||
mpp_allocator_free,
|
mpp_allocator_free,
|
||||||
|
mpp_allocator_import,
|
||||||
|
mpp_allocator_release,
|
||||||
};
|
};
|
||||||
|
|
||||||
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type)
|
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type)
|
||||||
|
@@ -23,6 +23,8 @@ typedef struct os_allocator_t {
|
|||||||
MPP_RET (*open)(void **ctx, size_t alignment);
|
MPP_RET (*open)(void **ctx, size_t alignment);
|
||||||
MPP_RET (*alloc)(void *ctx, MppBufferInfo *info);
|
MPP_RET (*alloc)(void *ctx, MppBufferInfo *info);
|
||||||
MPP_RET (*free)(void *ctx, MppBufferInfo *info);
|
MPP_RET (*free)(void *ctx, MppBufferInfo *info);
|
||||||
|
MPP_RET (*import)(void *ctx, MppBufferInfo *info);
|
||||||
|
MPP_RET (*release)(void *ctx, MppBufferInfo *info);
|
||||||
MPP_RET (*close)(void *ctx);
|
MPP_RET (*close)(void *ctx);
|
||||||
} os_allocator;
|
} os_allocator;
|
||||||
|
|
||||||
|
@@ -84,6 +84,8 @@ static os_allocator allocator_window = {
|
|||||||
os_allocator_open,
|
os_allocator_open,
|
||||||
os_allocator_alloc,
|
os_allocator_alloc,
|
||||||
os_allocator_free,
|
os_allocator_free,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
os_allocator_close,
|
os_allocator_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user