From e3f49b11aa5ef78f4cc4d0e8a99d61e095466146 Mon Sep 17 00:00:00 2001 From: ChenHengming Date: Fri, 9 Oct 2015 21:59:41 +0000 Subject: [PATCH] [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 --- mpp/mpp_buffer_impl.cpp | 16 +++++++++++--- osal/android/os_allocator.c | 18 +++++++++++++++ osal/inc/mpp_allocator.h | 2 ++ osal/linux/os_allocator.c | 4 ++++ osal/mpp_allocator.cpp | 44 +++++++++++++++++++++++++++++++++++-- osal/os_allocator.h | 2 ++ osal/window/os_allocator.c | 2 ++ 7 files changed, 83 insertions(+), 5 deletions(-) diff --git a/mpp/mpp_buffer_impl.cpp b/mpp/mpp_buffer_impl.cpp index 68e1f836..a320be11 100644 --- a/mpp/mpp_buffer_impl.cpp +++ b/mpp/mpp_buffer_impl.cpp @@ -73,6 +73,8 @@ MPP_RET deinit_buffer_no_lock(MppBufferImpl *buffer) if (group) { if (buffer->internal) group->alloc_api->free(group->allocator, &buffer->info); + else + group->alloc_api->release(group->allocator, &buffer->info); group->usage -= buffer->info.size; 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); if (group) { - if (NULL == info->ptr && info->fd == -1) { + if (info->fd == -1) { MPP_RET ret = group->alloc_api->alloc(group->allocator, info); if (MPP_OK != ret) { 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; } 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; @@ -220,7 +229,7 @@ MppBufferImpl *mpp_buffer_get_unused(MppBufferGroupImpl *p, size_t size) if (!list_empty(&p->list_unused)) { MppBufferImpl *pos, *n; 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; inc_buffer_ref_no_lock(buffer); 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 MppBufferImpl *pos, *n; 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); } } diff --git a/osal/android/os_allocator.c b/osal/android/os_allocator.c index 8fd75f73..9c58c4f1 100644 --- a/osal/android/os_allocator.c +++ b/osal/android/os_allocator.c @@ -87,6 +87,8 @@ static os_allocator allocator_normal = { os_allocator_normal_open, os_allocator_normal_alloc, os_allocator_normal_free, + NULL, + NULL, os_allocator_normal_close, }; @@ -237,6 +239,20 @@ MPP_RET os_allocator_ion_alloc(void *ctx, MppBufferInfo *info) 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) { allocator_ctx_ion *p = NULL; @@ -273,6 +289,8 @@ static os_allocator allocator_ion = { os_allocator_ion_open, os_allocator_ion_alloc, os_allocator_ion_free, + os_allocator_ion_import, + os_allocator_ion_release, os_allocator_ion_close, }; diff --git a/osal/inc/mpp_allocator.h b/osal/inc/mpp_allocator.h index 32c32ca3..ab680ad3 100644 --- a/osal/inc/mpp_allocator.h +++ b/osal/inc/mpp_allocator.h @@ -28,6 +28,8 @@ typedef struct MppAllocatorApi_t { MPP_RET (*alloc)(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; #ifdef __cplusplus diff --git a/osal/linux/os_allocator.c b/osal/linux/os_allocator.c index 2e8a813b..34b8e8cd 100644 --- a/osal/linux/os_allocator.c +++ b/osal/linux/os_allocator.c @@ -86,6 +86,8 @@ static os_allocator allocator_normal = { os_allocator_normal_open, os_allocator_normal_alloc, os_allocator_normal_free, + NULL, + NULL, os_allocator_normal_close, }; @@ -93,6 +95,8 @@ static os_allocator allocator_v4l2 = { os_allocator_normal_open, os_allocator_normal_alloc, os_allocator_normal_free, + NULL, + NULL, os_allocator_normal_close, }; diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index 9d98eafc..ed93dab0 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -29,7 +29,7 @@ MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *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); 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) { 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); return MPP_ERR_UNKNOW; } @@ -62,11 +62,51 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info) 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 = { sizeof(mpp_allocator_api), 1, mpp_allocator_alloc, mpp_allocator_free, + mpp_allocator_import, + mpp_allocator_release, }; MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type) diff --git a/osal/os_allocator.h b/osal/os_allocator.h index 0f3436b4..eb422928 100644 --- a/osal/os_allocator.h +++ b/osal/os_allocator.h @@ -23,6 +23,8 @@ typedef struct os_allocator_t { MPP_RET (*open)(void **ctx, size_t alignment); MPP_RET (*alloc)(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); } os_allocator; diff --git a/osal/window/os_allocator.c b/osal/window/os_allocator.c index f21e9fd4..aa3f8669 100644 --- a/osal/window/os_allocator.c +++ b/osal/window/os_allocator.c @@ -84,6 +84,8 @@ static os_allocator allocator_window = { os_allocator_open, os_allocator_alloc, os_allocator_free, + NULL, + NULL, os_allocator_close, };