diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 40a9096a..9d584911 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -6,13 +6,13 @@ endif() if(${ANDROID}) set(OS_DIR android) -set(MPP_ALLOCATOR allocator/allocator_ion.c ${DRM_FILES}) +set(MPP_ALLOCATOR allocator/allocator_std.c allocator/allocator_ion.c ${DRM_FILES}) elseif(${UNIX}) set(OS_DIR linux) -set(MPP_ALLOCATOR allocator/allocator_ion.c ${DRM_FILES}) +set(MPP_ALLOCATOR allocator/allocator_std.c allocator/allocator_ion.c ${DRM_FILES}) elseif(${WIN32}) set(OS_DIR window) -set(MPP_ALLOCATOR ) +set(MPP_ALLOCATOR allocator/allocator_std.c) else() message(SEND_ERROR "Can not found platform definistion ${CMAKE_SYSTEM}") endif() diff --git a/osal/allocator/allocator_drm.c b/osal/allocator/allocator_drm.c index 0dfd6695..b8d70ef6 100644 --- a/osal/allocator/allocator_drm.c +++ b/osal/allocator/allocator_drm.c @@ -360,9 +360,10 @@ MPP_RET os_allocator_drm_close(void *ctx) os_allocator allocator_drm = { os_allocator_drm_open, + os_allocator_drm_close, os_allocator_drm_alloc, os_allocator_drm_free, os_allocator_drm_import, os_allocator_drm_release, - os_allocator_drm_close, + NULL, }; diff --git a/osal/allocator/allocator_ion.c b/osal/allocator/allocator_ion.c index 91ccc279..3c343da4 100644 --- a/osal/allocator/allocator_ion.c +++ b/osal/allocator/allocator_ion.c @@ -442,10 +442,11 @@ MPP_RET os_allocator_ion_close(void *ctx) os_allocator allocator_ion = { os_allocator_ion_open, + os_allocator_ion_close, os_allocator_ion_alloc, os_allocator_ion_free, os_allocator_ion_import, os_allocator_ion_release, - os_allocator_ion_close, + NULL, }; diff --git a/osal/allocator/allocator_std.c b/osal/allocator/allocator_std.c new file mode 100644 index 00000000..aed0b82c --- /dev/null +++ b/osal/allocator/allocator_std.c @@ -0,0 +1,125 @@ +/* + * Copyright 2015 Rockchip Electronics Co. LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "os_mem.h" +#include "os_allocator.h" + +#include "mpp_mem.h" +#include "mpp_log.h" + +typedef struct { + size_t alignment; + RK_S32 fd_count; +} allocator_ctx; + +MPP_RET allocator_std_open(void **ctx, size_t alignment) +{ + MPP_RET ret = MPP_OK; + allocator_ctx *p = NULL; + + if (NULL == ctx) { + mpp_err_f("do not accept NULL input\n"); + return MPP_ERR_NULL_PTR; + } + + p = mpp_malloc(allocator_ctx, 1); + if (NULL == p) { + mpp_err_f("failed to allocate context\n"); + ret = MPP_ERR_MALLOC; + } else + p->alignment = alignment; + + p->fd_count = 0; + + *ctx = p; + return ret; +} + +MPP_RET allocator_std_alloc(void *ctx, MppBufferInfo *info) +{ + allocator_ctx *p = NULL; + + if (NULL == ctx) { + mpp_err_f("found NULL context input\n"); + return MPP_ERR_NULL_PTR; + } + + p = (allocator_ctx *)ctx; + info->fd = p->fd_count++; + return (MPP_RET)os_malloc(&info->ptr, p->alignment, info->size); +} + +MPP_RET allocator_std_free(void *ctx, MppBufferInfo *info) +{ + (void) ctx; + if (info->ptr) + os_free(info->ptr); + return MPP_OK; +} + +MPP_RET allocator_std_import(void *ctx, MppBufferInfo *info) +{ + allocator_ctx *p = (allocator_ctx *)ctx; + mpp_assert(ctx); + mpp_assert(info->ptr); + mpp_assert(info->size); + info->hnd = NULL; + info->fd = p->fd_count++; + return MPP_OK; +} + +MPP_RET allocator_std_release(void *ctx, MppBufferInfo *info) +{ + (void) ctx; + mpp_assert(info->ptr); + mpp_assert(info->size); + info->ptr = NULL; + info->size = 0; + info->hnd = NULL; + info->fd = -1; + return MPP_OK; +} + +MPP_RET allocator_std_mmap(void *ctx, MppBufferInfo *info) +{ + mpp_assert(ctx); + mpp_assert(info->ptr); + mpp_assert(info->size); + return MPP_OK; +} + +MPP_RET allocator_std_close(void *ctx) +{ + if (ctx) { + mpp_free(ctx); + return MPP_OK; + } + mpp_err_f("found NULL context input\n"); + return MPP_NOK; +} + +os_allocator allocator_std = { + allocator_std_open, + allocator_std_close, + allocator_std_alloc, + allocator_std_free, + allocator_std_import, + allocator_std_release, + allocator_std_mmap, +}; + diff --git a/osal/allocator/allocator_std.h b/osal/allocator/allocator_std.h new file mode 100644 index 00000000..0b427328 --- /dev/null +++ b/osal/allocator/allocator_std.h @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Rockchip Electronics Co. LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "os_allocator.h" + +extern os_allocator allocator_std; + diff --git a/osal/android/os_allocator.c b/osal/android/os_allocator.c index 370a9ec1..c3107765 100644 --- a/osal/android/os_allocator.c +++ b/osal/android/os_allocator.c @@ -14,112 +14,10 @@ * limitations under the License. */ -#include -#include -#include -#include - -#include "os_mem.h" -#include "os_allocator.h" +#include "allocator_std.h" #include "allocator_ion.h" #include "allocator_drm.h" -#include "mpp_mem.h" -#include "mpp_log.h" -#include "mpp_common.h" - -typedef struct { - RK_U32 alignment; - RK_S32 fd_count; -} allocator_ctx_normal; - -MPP_RET os_allocator_normal_open(void **ctx, size_t alignment) -{ - MPP_RET ret = MPP_OK; - allocator_ctx_normal *p = NULL; - - 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"); - ret = MPP_ERR_MALLOC; - } else - p->alignment = alignment; - - p->fd_count = 0; - - *ctx = p; - return ret; -} - -MPP_RET os_allocator_normal_alloc(void *ctx, MppBufferInfo *info) -{ - allocator_ctx_normal *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_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; - 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_assert(ctx); - mpp_assert(info->ptr); - mpp_assert(info->size); - info->hnd = NULL; - info->fd = p->fd_count++; - return MPP_OK; -} - -MPP_RET os_allocator_normal_release(void *ctx, MppBufferInfo *info) -{ - (void)ctx; - mpp_assert(info->ptr); - mpp_assert(info->size); - info->ptr = NULL; - info->size = 0; - info->hnd = NULL; - info->fd = -1; - return MPP_OK; -} - -MPP_RET os_allocator_normal_close(void *ctx) -{ - if (ctx) { - mpp_free(ctx); - return MPP_OK; - } - 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, - os_allocator_normal_free, - 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; @@ -127,7 +25,7 @@ MPP_RET os_allocator_get(os_allocator *api, MppBufferType type) switch (type) { case MPP_BUFFER_TYPE_NORMAL : case MPP_BUFFER_TYPE_V4L2 : { - *api = allocator_normal; + *api = allocator_std; } break; case MPP_BUFFER_TYPE_ION : { @@ -138,7 +36,7 @@ MPP_RET os_allocator_get(os_allocator *api, MppBufferType type) #ifdef HAVE_DRM *api = allocator_drm; #else - *api = allocator_normal; + *api = allocator_std; #endif } break; diff --git a/osal/linux/os_allocator.c b/osal/linux/os_allocator.c index 54fbe831..749bfcf9 100644 --- a/osal/linux/os_allocator.c +++ b/osal/linux/os_allocator.c @@ -14,145 +14,40 @@ * limitations under the License. */ -#include - -#include "os_mem.h" -#include "os_allocator.h" +#include "mpp_log.h" +#include "allocator_std.h" #include "allocator_drm.h" #include "allocator_ion.h" -#include "mpp_mem.h" -#include "mpp_log.h" - /* * Linux only support MPP_BUFFER_TYPE_NORMAL so far * we can support MPP_BUFFER_TYPE_V4L2 later */ -typedef struct { - size_t alignment; - RK_S32 fd_count; -} allocator_ctx; - -MPP_RET os_allocator_normal_open(void **ctx, size_t alignment) -{ - MPP_RET ret = MPP_OK; - allocator_ctx *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_ctx, 1); - if (NULL == p) { - mpp_err("os_allocator_open Linux failed to allocate context\n"); - ret = MPP_ERR_MALLOC; - } else - p->alignment = alignment; - - p->fd_count = 0; - - *ctx = p; - return ret; -} - -MPP_RET os_allocator_normal_alloc(void *ctx, MppBufferInfo *info) -{ - allocator_ctx *p = NULL; - - if (NULL == ctx) { - mpp_err("os_allocator_alloc Linux found NULL context input\n"); - return MPP_ERR_NULL_PTR; - } - - p = (allocator_ctx *)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; - if (info->ptr) - os_free(info->ptr); - return MPP_OK; -} - -MPP_RET os_allocator_normal_import(void *ctx, MppBufferInfo *info) -{ - allocator_ctx *p = (allocator_ctx *)ctx; - mpp_assert(ctx); - mpp_assert(info->ptr); - mpp_assert(info->size); - info->hnd = NULL; - info->fd = p->fd_count++; - return MPP_OK; -} - -MPP_RET os_allocator_normal_release(void *ctx, MppBufferInfo *info) -{ - (void) ctx; - mpp_assert(info->ptr); - mpp_assert(info->size); - info->ptr = NULL; - info->size = 0; - info->hnd = NULL; - info->fd = -1; - return MPP_OK; -} - -MPP_RET os_allocator_normal_close(void *ctx) -{ - if (ctx) { - mpp_free(ctx); - return MPP_OK; - } - 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, - os_allocator_normal_free, - os_allocator_normal_import, - os_allocator_normal_release, - os_allocator_normal_close, -}; - -static os_allocator allocator_v4l2 = { - os_allocator_normal_open, - os_allocator_normal_alloc, - os_allocator_normal_free, - 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; switch (type) { case MPP_BUFFER_TYPE_NORMAL : { - *api = allocator_normal; + *api = allocator_std; } break; case MPP_BUFFER_TYPE_ION : { #ifdef RKPLATFORM *api = allocator_ion; #else - *api = allocator_normal; + *api = allocator_std; #endif } break; case MPP_BUFFER_TYPE_V4L2 : { mpp_err("os_allocator_get Linux MPP_BUFFER_TYPE_V4L2 do not implement yet\n"); - *api = allocator_v4l2; + *api = allocator_std; } break; case MPP_BUFFER_TYPE_DRM : { #ifdef HAVE_DRM *api = allocator_drm; #else - *api = allocator_normal; + *api = allocator_std; #endif } break; default : { diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index 8eed8337..2e91e1d2 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -26,78 +26,80 @@ #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, MppBufferInfo *info) +typedef enum OsAllocatorApiId_e { + ALLOC_API_ALLOC, + ALLOC_API_FREE, + ALLOC_API_IMPORT, + ALLOC_API_RELEASE, + ALLOC_API_MMAP, + ALLOC_API_BUTT, +} OsAllocatorApiId; + +static MPP_RET mpp_allocator_api_wrapper(MppAllocator allocator, MppBufferInfo *info, OsAllocatorApiId id) { - if (NULL == allocator || NULL == info) { - mpp_err_f("invalid input: allocator %p info %p\n", - allocator, info); + if (NULL == allocator || NULL == info || id >= ALLOC_API_BUTT) { + mpp_err_f("invalid input: allocator %p info %p id %d\n", + allocator, info, id); return MPP_ERR_UNKNOW; } MPP_RET ret = MPP_NOK; MppAllocatorImpl *p = (MppAllocatorImpl *)allocator; + OsAllocatorFunc func; + MPP_ALLOCATOR_LOCK(p); - if (p->os_api.alloc && p->ctx) - ret = p->os_api.alloc(p->ctx, info); + switch (id) { + case ALLOC_API_ALLOC : { + func = p->os_api.alloc; + } break; + case ALLOC_API_FREE : { + func = p->os_api.free; + } break; + case ALLOC_API_IMPORT : { + func = p->os_api.import; + } break; + case ALLOC_API_RELEASE : { + func = p->os_api.release; + } break; + case ALLOC_API_MMAP : { + func = p->os_api.mmap; + } break; + default : { + func = NULL; + } break; + } + + if (func && p->ctx) + ret = func(p->ctx, info); + MPP_ALLOCATOR_UNLOCK(p); return ret; } +MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info) +{ + return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_ALLOC); +} + MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info) { - if (NULL == allocator || NULL == info) { - mpp_err_f("invalid input: allocator %p info %p\n", - allocator, info); - return MPP_ERR_UNKNOW; - } - - MPP_RET ret = MPP_NOK; - MppAllocatorImpl *p = (MppAllocatorImpl *)allocator; - MPP_ALLOCATOR_LOCK(p); - if (p->os_api.free && p->ctx) - ret = p->os_api.free(p->ctx, info); - MPP_ALLOCATOR_UNLOCK(p); - - return ret; + return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_FREE); } 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_NOK; - MppAllocatorImpl *p = (MppAllocatorImpl *)allocator; - MPP_ALLOCATOR_LOCK(p); - if (p->os_api.import && p->ctx) { - ret = p->os_api.import(p->ctx, info); - } - MPP_ALLOCATOR_UNLOCK(p); - - return ret; + return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_IMPORT); } - MPP_RET mpp_allocator_release(MppAllocator allocator, MppBufferInfo *info) { - if (NULL == allocator || NULL == info) { - mpp_err_f("invalid input: allocator %p info %p\n", - allocator, info); - return MPP_ERR_UNKNOW; - } + return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_RELEASE); +} - MPP_RET ret = MPP_NOK; - MppAllocatorImpl *p = (MppAllocatorImpl *)allocator; - MPP_ALLOCATOR_LOCK(p); - if (p->os_api.release && p->ctx) { - ret = p->os_api.release(p->ctx, info); - } - MPP_ALLOCATOR_UNLOCK(p); - - return ret; +MPP_RET mpp_allocator_mmap(MppAllocator allocator, MppBufferInfo *info) +{ + return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_MMAP); } static MppAllocatorApi mpp_allocator_api = { @@ -107,7 +109,7 @@ static MppAllocatorApi mpp_allocator_api = { mpp_allocator_free, mpp_allocator_import, mpp_allocator_release, - NULL, + mpp_allocator_mmap, }; MPP_RET mpp_allocator_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type) diff --git a/osal/os_allocator.h b/osal/os_allocator.h index 71989ab0..b349f008 100644 --- a/osal/os_allocator.h +++ b/osal/os_allocator.h @@ -19,13 +19,17 @@ #include "mpp_allocator.h" +typedef MPP_RET (*OsAllocatorFunc)(void *ctx, MppBufferInfo *info); + 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); + + OsAllocatorFunc alloc; + OsAllocatorFunc free; + OsAllocatorFunc import; + OsAllocatorFunc release; + OsAllocatorFunc mmap; } os_allocator; #ifdef __cplusplus diff --git a/osal/window/os_allocator.c b/osal/window/os_allocator.c index 2a661978..64c69045 100644 --- a/osal/window/os_allocator.c +++ b/osal/window/os_allocator.c @@ -14,108 +14,13 @@ * limitations under the License. */ -#include "os_mem.h" #include "os_allocator.h" - -#include "mpp_mem.h" -#include "mpp_log.h" +#include "allocator_std.h" /* * window only support MPP_BUFFER_TYPE_NORMAL */ -typedef struct { - size_t alignment; - RK_S32 fd_count; -} allocator_ctx; - -MPP_RET os_allocator_open(void **ctx, size_t alignment) -{ - MPP_RET ret = MPP_OK; - allocator_ctx *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_ctx, 1); - if (NULL == p) { - mpp_err("os_allocator_open Window failed to allocate context\n"); - ret = MPP_ERR_MALLOC; - } else - p->alignment = alignment; - - p->fd_count = 0; - - *ctx = p; - return ret; -} - -MPP_RET os_allocator_alloc(void *ctx, MppBufferInfo *info) -{ - allocator_ctx *p = NULL; - - if (NULL == ctx) { - mpp_err("os_allocator_alloc Window found NULL context input\n"); - return MPP_ERR_NULL_PTR; - } - - p = (allocator_ctx *)ctx; - info->fd = p->fd_count++; - return (MPP_RET)os_malloc(&info->ptr, p->alignment, info->size); -} - -MPP_RET os_allocator_free(void *ctx, MppBufferInfo *info) -{ - (void) ctx; - if (info->ptr) - os_free(info->ptr); - return MPP_OK; -} - -MPP_RET os_allocator_import(void *ctx, MppBufferInfo *info) -{ - allocator_ctx *p = (allocator_ctx *)ctx; - mpp_assert(ctx); - mpp_assert(info->ptr); - mpp_assert(info->size); - info->hnd = NULL; - info->fd = p->fd_count++; - return MPP_OK; -} - -MPP_RET os_allocator_release(void *ctx, MppBufferInfo *info) -{ - (void) ctx; - mpp_assert(info->ptr); - mpp_assert(info->size); - info->ptr = NULL; - info->size = 0; - info->hnd = NULL; - info->fd = -1; - return MPP_OK; -} - -MPP_RET os_allocator_close(void *ctx) -{ - if (ctx) { - mpp_free(ctx); - return MPP_OK; - } - mpp_err("os_allocator_close Window found NULL context input\n"); - return MPP_NOK; -} - -static os_allocator allocator_window = { - os_allocator_open, - os_allocator_alloc, - os_allocator_free, - os_allocator_import, - os_allocator_release, - os_allocator_close, -}; - MPP_RET os_allocator_get(os_allocator *api, MppBufferType type) { MPP_RET ret = MPP_OK; @@ -123,7 +28,7 @@ MPP_RET os_allocator_get(os_allocator *api, MppBufferType type) case MPP_BUFFER_TYPE_NORMAL : case MPP_BUFFER_TYPE_ION : case MPP_BUFFER_TYPE_V4L2 : { - *api = allocator_window; + *api = allocator_std; } break; default : { ret = MPP_NOK;