From c0b913d3b9af79ce3ff629f30d5916be3505fb05 Mon Sep 17 00:00:00 2001 From: ChenHengming Date: Tue, 18 Aug 2015 12:57:16 +0000 Subject: [PATCH] [mpp_buffer_impl]: add MppBufferGroup module to mpp_buffer_impl git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@122 6e48237b-75ef-9749-8fc9-41990f28c85a --- inc/mpp_buffer.h | 12 ++-- mpp/CMakeLists.txt | 9 +-- mpp/mpp_buffer.cpp | 18 ++++-- mpp/mpp_buffer_impl.cpp | 130 ++++++++++++++++++++++++++++++++++++++++ mpp/mpp_buffer_impl.h | 82 +++++++++++++++++++++++++ osal/CMakeLists.txt | 4 +- osal/inc/mpp_common.h | 9 ++- osal/inc/mpp_mem.h | 5 -- osal/mpp_log.cpp | 1 + osal/mpp_mem.cpp | 3 +- 10 files changed, 246 insertions(+), 27 deletions(-) create mode 100644 mpp/mpp_buffer_impl.cpp create mode 100644 mpp/mpp_buffer_impl.h diff --git a/inc/mpp_buffer.h b/inc/mpp_buffer.h index e1aa5b53..195e01c2 100644 --- a/inc/mpp_buffer.h +++ b/inc/mpp_buffer.h @@ -87,11 +87,11 @@ typedef struct { #define mpp_buffer_commit(...) _mpp_buffer_commit(MODULE_TAG, ## __VA_ARGS__) #define mpp_buffer_get(...) _mpp_buffer_get(MODULE_TAG, ## __VA_ARGS__) -#define mpp_buffer_put(...) _mpp_buffer_put(MODULE_TAG, ## __VA_ARGS__) -#define mpp_buffer_inc_ref(...) _mpp_buffer_inc_ref(MODULE_TAG, ## __VA_ARGS__) +#define mpp_buffer_put(...) _mpp_buffer_put(## __VA_ARGS__) +#define mpp_buffer_inc_ref(...) _mpp_buffer_inc_ref(## __VA_ARGS__) #define mpp_buffer_group_get(...) _mpp_buffer_group_get(MODULE_TAG, ## __VA_ARGS__) -#define mpp_buffer_group_put(...) _mpp_buffer_group_put(MODULE_TAG, ## __VA_ARGS__) +#define mpp_buffer_group_put(...) _mpp_buffer_group_put(## __VA_ARGS__) #ifdef __cplusplus extern "C" { @@ -104,11 +104,11 @@ extern "C" { */ MPP_RET _mpp_buffer_commit(const char *tag, MppBufferGroup group, MppBufferCommit *buffer); MPP_RET _mpp_buffer_get(const char *tag, MppBufferGroup group, MppBuffer *buffer, size_t size); -MPP_RET _mpp_buffer_put(const char *tag, MppBuffer *buffer); -MPP_RET _mpp_buffer_inc_ref(const char *tag, MppBuffer buffer); +MPP_RET _mpp_buffer_put(MppBuffer *buffer); +MPP_RET _mpp_buffer_inc_ref(MppBuffer buffer); MPP_RET _mpp_buffer_group_get(const char *tag, MppBufferGroup *group, MppBufferType type); -MPP_RET _mpp_buffer_group_put(const char *tag, MppBufferGroup *group); +MPP_RET _mpp_buffer_group_put(MppBufferGroup *group); #ifdef __cplusplus } diff --git a/mpp/CMakeLists.txt b/mpp/CMakeLists.txt index b05c3cfd..0f7732c1 100644 --- a/mpp/CMakeLists.txt +++ b/mpp/CMakeLists.txt @@ -21,10 +21,11 @@ add_subdirectory(hal) # ---------------------------------------------------------------------------- add_library(mpp STATIC mpp_info.cpp - mpp_buffer.cpp - mpp_packet.cpp - mpi_impl.cpp - mpi.cpp + mpp_buffer_impl.cpp + mpp_buffer.cpp + mpp_packet.cpp + mpi_impl.cpp + mpi.cpp ) add_subdirectory(legacy) diff --git a/mpp/mpp_buffer.cpp b/mpp/mpp_buffer.cpp index cc7797db..07816d36 100644 --- a/mpp/mpp_buffer.cpp +++ b/mpp/mpp_buffer.cpp @@ -20,8 +20,7 @@ #include "mpp_log.h" #include "mpp_mem.h" -#include "mpp_list.h" -#include "mpp_buffer.h" +#include "mpp_buffer_impl.h" MPP_RET _mpp_buffer_commit(const char *tag, MppBufferGroup group, MppBufferCommit *buffer) { @@ -33,12 +32,12 @@ MPP_RET _mpp_buffer_get(const char *tag, MppBufferGroup group, MppBuffer *buffer return MPP_OK; } -MPP_RET _mpp_buffer_put(const char *tag, MppBuffer *buffer) +MPP_RET _mpp_buffer_put(MppBuffer *buffer) { return MPP_OK; } -MPP_RET _mpp_buffer_inc_ref(const char *tag, MppBuffer buffer) +MPP_RET _mpp_buffer_inc_ref(MppBuffer buffer) { return MPP_OK; } @@ -46,10 +45,17 @@ MPP_RET _mpp_buffer_inc_ref(const char *tag, MppBuffer buffer) MPP_RET _mpp_buffer_group_get(const char *tag, MppBufferGroup *group, MppBufferType type) { - return MPP_OK; + if (NULL == group || type >= MPP_BUFFER_TYPE_BUTT) { + mpp_err("mpp_buffer_group_get input invalid group %p type %d\n", + group, type); + return MPP_ERR_UNKNOW; + } + + MppBufferGroupImpl *p; + return mpp_buffer_group_create(&p, tag, type); } -MPP_RET _mpp_buffer_group_put(const char *tag, MppBufferGroup *group) +MPP_RET _mpp_buffer_group_put(MppBufferGroup *group) { return MPP_OK; } diff --git a/mpp/mpp_buffer_impl.cpp b/mpp/mpp_buffer_impl.cpp new file mode 100644 index 00000000..59468b93 --- /dev/null +++ b/mpp/mpp_buffer_impl.cpp @@ -0,0 +1,130 @@ +/* + * Copyright 2010 Rockchip Electronics S.LSI 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. + */ + +#define MODULE_TAG "mpp_buffer" + +#include + +#include "mpp_log.h" +#include "mpp_mem.h" +#include "mpp_common.h" +#include "mpp_buffer.h" +#include "mpp_buffer_impl.h" + +#define MPP_BUFFER_SERVICE_LOCK() pthread_mutex_lock(&services.lock) +#define MPP_BUFFER_SERVICE_UNLOCK() pthread_mutex_unlock(&services.lock) + +typedef struct { + pthread_mutex_t lock; + RK_U32 group_id; + RK_U32 group_count; + + struct list_head list_group; + + // list for used buffer which do not have group + struct list_head list_orphan; +} MppBufferService; + +static MppBufferService services = +{ + PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, + 0, + 0, + LIST_HEAD_INIT(services.list_group), + LIST_HEAD_INIT(services.list_orphan), +}; + +static RK_U32 add_group_to_service(MppBufferGroupImpl *p) +{ + list_add_tail(&p->list_group, &services.list_group); + services.group_count++; + return services.group_id++; +} + +static void del_group_from_service(MppBufferGroupImpl *p) +{ + list_del_init(&p->list_group); + services.group_count--; +} + +static void add_orphan_to_service(MppBufferImpl *p) +{ + list_add_tail(&p->list_status, &services.list_orphan); +} + +static void del_orphan_from_service(MppBufferImpl *p) +{ + list_del_init(&p->list_status); +} + +MPP_RET mpp_buffer_group_create(MppBufferGroupImpl **group, const char *tag, MppBufferType type) +{ + MppBufferGroupImpl *p = mpp_malloc(MppBufferGroupImpl, 1); + if (NULL == p) { + mpp_err("mpp_buffer_group_get failed to allocate context\n"); + *group = NULL; + return MPP_ERR_MALLOC; + } + + MPP_BUFFER_SERVICE_LOCK(); + + INIT_LIST_HEAD(&p->list_group); + INIT_LIST_HEAD(&p->list_used); + INIT_LIST_HEAD(&p->list_unused); + + RK_U32 group_id = add_group_to_service(p); + + snprintf(p->tag, MPP_TAG_SIZE, "%s_%d", tag, group_id); + p->type = type; + p->group_id = group_id; + + MPP_BUFFER_SERVICE_UNLOCK(); + *group = p; + + return MPP_OK; +} + +MPP_RET mpp_buffer_group_destroy(MppBufferGroupImpl *p) +{ + if (NULL == p) { + mpp_err("mpp_buffer_group_destroy found NULL pointer\n"); + return MPP_ERR_NULL_PTR; + } + + MPP_BUFFER_SERVICE_LOCK(); + + del_group_from_service(p); + + // remove unused list + if (!list_empty(&p->list_unused)) { + MppBufferImpl *pos, *n; + list_for_each_entry_safe(pos, n, &p->list_unused, MppBufferImpl, list_status) { + mpp_free(pos); + } + } + + if (!list_empty(&p->list_used)) { + MppBufferImpl *pos, *n; + list_for_each_entry_safe(pos, n, &p->list_used, MppBufferImpl, list_status) { + add_orphan_to_service(pos); + } + } + + MPP_BUFFER_SERVICE_UNLOCK(); + + return MPP_OK; +} + diff --git a/mpp/mpp_buffer_impl.h b/mpp/mpp_buffer_impl.h new file mode 100644 index 00000000..5d30416b --- /dev/null +++ b/mpp/mpp_buffer_impl.h @@ -0,0 +1,82 @@ +/* + * Copyright 2010 Rockchip Electronics S.LSI 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. + */ + +#ifndef __MPP_BUFFER_IMPL_H__ +#define __MPP_BUFFER_IMPL_H__ + +#include "mpp_list.h" +#include "mpp_common.h" +#include "mpp_buffer.h" +#include "mpp_thread.h" + +#define MPP_BUF_DBG_FUNCTION (0x00000001) + +#define mpp_buf_dbg(flag, fmt, ...) mpp_dbg(mpp_buffer_debug, flag, fmt, ## __VA_ARGS__) + +#define MPP_BUF_FUNCTION_ENTER() mpp_buf_dbg(MPP_BUF_DBG_FUNCTION, "%s enter\n", __FUNCTION__) +#define MPP_BUF_FUNCTION_LEAVE() mpp_buf_dbg(MPP_BUF_DBG_FUNCTION, "%s leave\n", __FUNCTION__) +#define MPP_BUF_FUNCTION_LEAVE_OK() mpp_buf_dbg(MPP_BUF_DBG_FUNCTION, "%s success\n", __FUNCTION__) +#define MPP_BUF_FUNCTION_LEAVE_FAIL() mpp_buf_dbg(MPP_BUF_DBG_FUNCTION, "%s failed\n", __FUNCTION__) + +typedef union MppBufferData_t MppBufferData; +typedef struct MppBufferImpl_t MppBufferImpl; +typedef struct MppBufferGroupImpl_t MppBufferGroupImpl; + +union MppBufferData_t { + void *ptr; + RK_S32 fd; +}; + +struct MppBufferImpl_t { + char tag[MPP_TAG_SIZE]; + // use index instead of pointer to avoid invalid pointer + RK_U32 group_id; + MppBufferType type; + + size_t size; + MppBufferData data; + RK_S32 ref_count; + + struct list_head list_status; +}; + +struct MppBufferGroupImpl_t { + char tag[MPP_TAG_SIZE]; + RK_U32 group_id; + MppBufferType type; + + // link to the other MppBufferGroupImpl + struct list_head list_group; + + // link to list_status in MppBufferImpl + struct list_head list_used; + struct list_head list_unused; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +extern RK_U32 mpp_buffer_debug; + +MPP_RET mpp_buffer_group_create(MppBufferGroupImpl **group, const char *tag, MppBufferType type); +MPP_RET mpp_buffer_group_destroy(MppBufferGroupImpl *p); + +#ifdef __cplusplus +} +#endif + +#endif /*__MPP_BUFFER_IMPL_H__*/ diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 59582986..627c259d 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -12,8 +12,8 @@ endif() include_directories(.) add_library(osal STATIC - mpp_common.cpp - mpp_time.cpp + mpp_common.cpp + mpp_time.cpp mpp_list.cpp mpp_mem.cpp mpp_env.cpp diff --git a/osal/inc/mpp_common.h b/osal/inc/mpp_common.h index 6eaf3c1e..1fadc402 100644 --- a/osal/inc/mpp_common.h +++ b/osal/inc/mpp_common.h @@ -19,6 +19,8 @@ #include "rk_type.h" +#define MPP_TAG_SIZE 32 + #define MPP_MAX(a, b) ((a) > (b) ? (a) : (b)) #define MPP_MAX3(a, b, c) MPP_MAX(MPP_MAX(a,b),c) #define MPP_MAX4(a, b, c, d) MPP_MAX((a), MPP_MAX3((b), (c), (d))) @@ -34,12 +36,13 @@ #if defined(_WIN32) && !defined(__MINGW32CE__) -#define fseeko _fseeki64 +#include +#define snprintf _snprintf +#define fseeko _fseeki64 #elif defined(__MINGW32CE__) -#define fseeko fseeko64 +#define fseeko fseeko64 #endif - #ifdef __cplusplus extern "C" { #endif diff --git a/osal/inc/mpp_mem.h b/osal/inc/mpp_mem.h index ad976582..6dfffee8 100644 --- a/osal/inc/mpp_mem.h +++ b/osal/inc/mpp_mem.h @@ -49,10 +49,5 @@ void mpp_osal_free(void *ptr); } #endif -// buffer manipulation function -#if defined(_WIN32) -#define snprintf _snprintf -#endif - #endif /*__MPP_MEM_H__*/ diff --git a/osal/mpp_log.cpp b/osal/mpp_log.cpp index ce6d83ab..94a77618 100644 --- a/osal/mpp_log.cpp +++ b/osal/mpp_log.cpp @@ -22,6 +22,7 @@ #include "mpp_log.h" #include "mpp_mem.h" +#include "mpp_common.h" #include "os_log.h" diff --git a/osal/mpp_mem.cpp b/osal/mpp_mem.cpp index 3171bdab..c996adca 100644 --- a/osal/mpp_mem.cpp +++ b/osal/mpp_mem.cpp @@ -25,6 +25,7 @@ #include "mpp_env.h" #include "mpp_mem.h" #include "mpp_list.h" +#include "mpp_common.h" #include "os_mem.h" @@ -49,7 +50,7 @@ struct mem_node { size_t size; /* memory node extra information */ - char tag[32]; + char tag[MPP_TAG_SIZE]; }; static void get_osal_mem_flag()