From 2e5aafaf44b16ecbf385a24f2a0e52725a5dca47 Mon Sep 17 00:00:00 2001 From: ChenHengming Date: Thu, 20 Aug 2015 03:23:36 +0000 Subject: [PATCH] [osal]: add normal mode mpp_buffer path to test and fix a lot of bug git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@134 6e48237b-75ef-9749-8fc9-41990f28c85a --- mpp/mpp_buffer.cpp | 2 +- mpp/mpp_buffer_impl.cpp | 23 ++++++----- osal/android/os_allocator.c | 25 +++++++++++- osal/inc/mpp_allocator.h | 2 +- osal/linux/os_allocator.c | 48 ++++++++++++++++++---- osal/mpp_allocator.cpp | 33 ++++------------ osal/os_allocator.h | 4 +- osal/window/os_allocator.c | 45 +++++++++++++++++---- test/mpp_buffer_test.c | 79 +++++++++++++++++++++++++------------ 9 files changed, 179 insertions(+), 82 deletions(-) diff --git a/mpp/mpp_buffer.cpp b/mpp/mpp_buffer.cpp index e9ce9fe8..3ec4c6e5 100644 --- a/mpp/mpp_buffer.cpp +++ b/mpp/mpp_buffer.cpp @@ -54,7 +54,7 @@ MPP_RET mpp_buffer_get_with_tag(const char *tag, MppBufferGroup group, MppBuffer if (NULL == buf) { // if failed try init a new buffer mpp_buffer_create(tag, tmp->group_id, size, NULL); - mpp_buffer_get_unused(tmp, size); + buf = mpp_buffer_get_unused(tmp, size); } *buffer = buf; return (buf) ? (MPP_OK) : (MPP_NOK); diff --git a/mpp/mpp_buffer_impl.cpp b/mpp/mpp_buffer_impl.cpp index 7248f0ff..433152a6 100644 --- a/mpp/mpp_buffer_impl.cpp +++ b/mpp/mpp_buffer_impl.cpp @@ -139,25 +139,28 @@ MPP_RET mpp_buffer_create(const char *tag, RK_U32 group_id, size_t size, MppBuff MppBufferGroupImpl *group = SEARCH_GROUP_NORMAL(group_id); if (group) { if (NULL == data) { - group->alloc_api->alloc(group->allocator, &data, size); + MppBufferData tmp; + MPP_RET ret = group->alloc_api->alloc(group->allocator, &tmp, size); + if (MPP_OK != ret) { + mpp_err("mpp_buffer_create failed to create buffer with size %d\n", size); + mpp_free(p); + MPP_BUFFER_SERVICE_UNLOCK(); + return MPP_ERR_MALLOC; + } + + p->data = tmp; p->mode = MPP_BUFFER_MODE_NATIVE; } else { + p->data = *data; p->mode = MPP_BUFFER_MODE_COMMIT; } - if (NULL == data) { - mpp_err("mpp_buffer_create failed to create buffer with size %d\n", size); - mpp_free(p); - MPP_BUFFER_SERVICE_UNLOCK(); - return MPP_ERR_MALLOC; - } - if (NULL == tag) { + if (NULL == tag) tag = group->tag; - } + strncpy(p->tag, tag, sizeof(p->tag)); p->group_id = group_id; p->size = size; - p->data = *data; p->used = 0; p->ref_count = 0; INIT_LIST_HEAD(&p->list_status); diff --git a/osal/android/os_allocator.c b/osal/android/os_allocator.c index d74b6d80..bb6a8919 100644 --- a/osal/android/os_allocator.c +++ b/osal/android/os_allocator.c @@ -15,13 +15,34 @@ */ #include +#include + #include "os_mem.h" #include "os_allocator.h" -int os_allocator_open(void **ctx) +#include "mpp_mem.h" +#include "mpp_log.h" + +typedef struct { + RK_S32 ion_client; + RK_U32 align; +} allocator_ion; + +int os_allocator_open(void **ctx, size_t alignment) { - if (ctx) + if (NULL == ctx) { + mpp_err("os_allocator_open Android do not accept NULL input\n"); + return MPP_ERR_NULL_PTR; + } + + allocator_ion *p = mpp_malloc(allocator_ion, 1); + if (NULL == p) { *ctx = NULL; + mpp_err("os_allocator_open Android failed to allocate context\n"); + return MPP_ERR_MALLOC; + } + p->ion_client = + *ctx = p; return 0; } diff --git a/osal/inc/mpp_allocator.h b/osal/inc/mpp_allocator.h index 46bf6bcc..e78ed2b8 100644 --- a/osal/inc/mpp_allocator.h +++ b/osal/inc/mpp_allocator.h @@ -26,7 +26,7 @@ typedef struct { RK_U32 size; RK_U32 version; - MPP_RET (*alloc)(MppAllocator allocator, MppBufferData **data, size_t size); + MPP_RET (*alloc)(MppAllocator allocator, MppBufferData *data, size_t size); MPP_RET (*free)(MppAllocator allocator, MppBufferData *data); } MppAllocatorApi; diff --git a/osal/linux/os_allocator.c b/osal/linux/os_allocator.c index d74b6d80..c119640a 100644 --- a/osal/linux/os_allocator.c +++ b/osal/linux/os_allocator.c @@ -14,21 +14,50 @@ * limitations under the License. */ -#include +#include + #include "os_mem.h" #include "os_allocator.h" -int os_allocator_open(void **ctx) +#include "mpp_mem.h" +#include "mpp_log.h" + +typedef struct { + size_t alignment; +} allocator_impl; + +int os_allocator_open(void **ctx, size_t alignment) { - if (ctx) + allocator_impl *p = NULL; + + if (NULL == ctx) { + mpp_err("os_allocator_open Linux do not accept NULL input\n"); + return MPP_ERR_NULL_PTR; + } + + p = mpp_malloc(allocator_impl, 1); + if (NULL == p) { *ctx = NULL; - return 0; + mpp_err("os_allocator_open Linux failed to allocate context\n"); + return MPP_ERR_MALLOC; + } + + p->alignment = alignment; + *ctx = p; + return MPP_OK; } -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size) +int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) { - (void) ctx; - return os_malloc(&data->ptr, alignment, size); + allocator_impl *p = NULL; + + if (NULL == ctx) { + mpp_err("os_allocator_alloc Linux found NULL context input\n"); + return MPP_ERR_NULL_PTR; + } + + p = (allocator_impl *)ctx; + return os_malloc(&data->ptr, p->alignment, size); } void os_allocator_free(void *ctx, MppBufferData *data) @@ -39,6 +68,9 @@ void os_allocator_free(void *ctx, MppBufferData *data) void os_allocator_close(void *ctx) { - (void) ctx; + if (ctx) + mpp_free(ctx); + else + mpp_err("os_allocator_close Linux found NULL context input\n"); } diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index 3ded08c5..9548037f 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -26,32 +26,17 @@ #define MPP_ALLOCATOR_LOCK(p) pthread_mutex_lock(&(p)->lock); #define MPP_ALLOCATOR_UNLOCK(p) pthread_mutex_unlock(&(p)->lock); -MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferData **data, size_t size) +MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferData *data, size_t size) { - if (NULL == allocator || NULL == data) { - mpp_err("mpp_allocator_alloc invalid input: allocator %p data %p\n", - allocator, data); + if (NULL == allocator || NULL == data || 0 == size) { + mpp_err("mpp_allocator_alloc invalid input: allocator %p data %p size %d\n", + allocator, data, size); return MPP_ERR_UNKNOW; } - if (0 == size) { - *data = NULL; - return MPP_NOK; - } - - MppBufferData *pdata = mpp_malloc(MppBufferData, 1); - if (NULL == pdata) { - mpp_err("mpp_allocator_alloc failed to alloc MppBufferData\n"); - *data = NULL; - return MPP_ERR_MALLOC; - } - MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator; MPP_ALLOCATOR_LOCK(palloc); - - int ret = os_allocator_alloc(palloc->allocator, pdata, palloc->alignment, size); - *data = pdata; - + int ret = os_allocator_alloc(palloc->allocator, data, size); MPP_ALLOCATOR_UNLOCK(palloc); return (0 == ret) ? (MPP_OK) : (MPP_NOK); @@ -70,8 +55,6 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data) os_allocator_free(palloc->allocator, data); MPP_ALLOCATOR_UNLOCK(palloc); - mpp_free(data); - return MPP_OK; } @@ -96,14 +79,14 @@ MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api) return MPP_ERR_NULL_PTR; } - os_allocator_open(&palloc->allocator); + palloc->alignment = SZ_4K; + os_allocator_open(&palloc->allocator, palloc->alignment); + papi->size = sizeof(papi->size); papi->version = 1; papi->alloc = mpp_allocator_alloc; papi->free = mpp_allocator_free; - palloc->api = papi; - palloc->alignment = SZ_4K; pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); diff --git a/osal/os_allocator.h b/osal/os_allocator.h index 58f957af..8e513dea 100644 --- a/osal/os_allocator.h +++ b/osal/os_allocator.h @@ -23,8 +23,8 @@ extern "C" { #endif -int os_allocator_open(void **ctx); -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size); +int os_allocator_open(void **ctx, size_t alignment); +int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size); void os_allocator_free(void *ctx, MppBufferData *data); void os_allocator_close(void *ctx); diff --git a/osal/window/os_allocator.c b/osal/window/os_allocator.c index bb45432a..ad906f95 100644 --- a/osal/window/os_allocator.c +++ b/osal/window/os_allocator.c @@ -17,17 +17,45 @@ #include "os_mem.h" #include "os_allocator.h" -int os_allocator_open(void **ctx) +#include "mpp_mem.h" +#include "mpp_log.h" + +typedef struct { + size_t alignment; +} allocator_impl; + +int os_allocator_open(void **ctx, size_t alignment) { - if (ctx) + allocator_impl *p = NULL; + + if (NULL == ctx) { + mpp_err("os_allocator_open Window do not accept NULL input\n"); + return MPP_ERR_NULL_PTR; + } + + p = mpp_malloc(allocator_impl, 1); + if (NULL == p) { *ctx = NULL; - return 0; + mpp_err("os_allocator_open Window failed to allocate context\n"); + return MPP_ERR_MALLOC; + } + + p->alignment = alignment; + *ctx = p; + return MPP_OK; } -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size) +int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) { - (void) ctx; - return os_malloc(&data->ptr, alignment, size); + allocator_impl *p = NULL; + + if (NULL == ctx) { + mpp_err("os_allocator_alloc Window found NULL context input\n"); + return MPP_ERR_NULL_PTR; + } + + p = (allocator_impl *)ctx; + return os_malloc(&data->ptr, p->alignment, size); } void os_allocator_free(void *ctx, MppBufferData *data) @@ -38,6 +66,9 @@ void os_allocator_free(void *ctx, MppBufferData *data) void os_allocator_close(void *ctx) { - (void) ctx; + if (ctx) + mpp_free(ctx); + else + mpp_err("os_allocator_close Window found NULL context input\n"); } diff --git a/test/mpp_buffer_test.c b/test/mpp_buffer_test.c index abd44ee4..74d995eb 100644 --- a/test/mpp_buffer_test.c +++ b/test/mpp_buffer_test.c @@ -21,23 +21,26 @@ #include "mpp_log.h" #include "mpp_buffer.h" -#define MPP_BUFFER_TEST_SIZE 1024 -#define MPP_BUFFER_TEST_COUNT 10 +#define MPP_BUFFER_TEST_SIZE (SZ_1K*4) +#define MPP_BUFFER_TEST_COMMIT_COUNT 10 +#define MPP_BUFFER_TEST_NORMAL_COUNT 10 int main() { MPP_RET ret = MPP_OK; MppBufferCommit commit; MppBufferGroup group = NULL; - MppBuffer buffer[MPP_BUFFER_TEST_COUNT]; - void *ptr[MPP_BUFFER_TEST_COUNT]; + MppBuffer commit_buffer[MPP_BUFFER_TEST_COMMIT_COUNT]; + void *commit_ptr[MPP_BUFFER_TEST_COMMIT_COUNT]; + MppBuffer normal_buffer[MPP_BUFFER_TEST_NORMAL_COUNT]; size_t size = MPP_BUFFER_TEST_SIZE; RK_S32 i; mpp_log("mpp_buffer_test start\n"); - memset(ptr, 0, sizeof(ptr)); - memset(buffer, 0, sizeof(buffer)); + memset(commit_ptr, 0, sizeof(commit_ptr)); + memset(commit_buffer, 0, sizeof(commit_buffer)); + memset(normal_buffer, 0, sizeof(normal_buffer)); ret = mpp_buffer_group_get(&group, MPP_BUFFER_TYPE_NORMAL); if (MPP_OK != ret) { @@ -45,17 +48,19 @@ int main() goto MPP_BUFFER_failed; } + mpp_log("mpp_buffer_test commit mode start\n"); + commit.type = MPP_BUFFER_TYPE_NORMAL; commit.size = size; - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - ptr[i] = malloc(size); - if (NULL == ptr[i]) { + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + commit_ptr[i] = malloc(size); + if (NULL == commit_ptr[i]) { mpp_err("mpp_buffer_test malloc failed\n"); goto MPP_BUFFER_failed; } - commit.data.ptr = ptr[i]; + commit.data.ptr = commit_ptr[i]; ret = mpp_buffer_commit(group, &commit); if (MPP_OK != ret) { @@ -64,29 +69,51 @@ int main() } } - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - ret = mpp_buffer_get(group, &buffer[i], size); + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + ret = mpp_buffer_get(group, &commit_buffer[i], size); if (MPP_OK != ret) { - mpp_err("mpp_buffer_test mpp_buffer_get failed\n"); + mpp_err("mpp_buffer_test mpp_buffer_get commit mode failed\n"); goto MPP_BUFFER_failed; } } - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - ret = mpp_buffer_put(&buffer[i]); + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + ret = mpp_buffer_put(&commit_buffer[i]); if (MPP_OK != ret) { - mpp_err("mpp_buffer_test mpp_buffer_put failed\n"); + mpp_err("mpp_buffer_test mpp_buffer_put commit mode failed\n"); goto MPP_BUFFER_failed; } } - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - if (ptr[i]) { - free(ptr[i]); - ptr[i] = NULL; + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + if (commit_ptr[i]) { + free(commit_ptr[i]); + commit_ptr[i] = NULL; } } + mpp_log("mpp_buffer_test commit mode success\n"); + + mpp_log("mpp_buffer_test normal mode start\n"); + + for (i = 0; i < MPP_BUFFER_TEST_NORMAL_COUNT; i++) { + ret = mpp_buffer_get(group, &normal_buffer[i], (i+1)*SZ_1K); + if (MPP_OK != ret) { + mpp_err("mpp_buffer_test mpp_buffer_get mode normal failed\n"); + goto MPP_BUFFER_failed; + } + } + + for (i = 0; i < MPP_BUFFER_TEST_NORMAL_COUNT; i++) { + ret = mpp_buffer_put(&normal_buffer[i]); + if (MPP_OK != ret) { + mpp_err("mpp_buffer_test mpp_buffer_get mode normal failed\n"); + goto MPP_BUFFER_failed; + } + } + + mpp_log("mpp_buffer_test normal mode success\n"); + if (group) mpp_buffer_group_put(&group); @@ -94,14 +121,14 @@ int main() return ret; MPP_BUFFER_failed: - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - mpp_buffer_put(&buffer[i]); + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + mpp_buffer_put(&commit_buffer[i]); } - for (i = 0; i < MPP_BUFFER_TEST_COUNT; i++) { - if (ptr[i]) { - free(ptr[i]); - ptr[i] = NULL; + for (i = 0; i < MPP_BUFFER_TEST_COMMIT_COUNT; i++) { + if (commit_ptr[i]) { + free(commit_ptr[i]); + commit_ptr[i] = NULL; } }