From 062c17526523f22ca0ecd7c5756813e53c6b37e3 Mon Sep 17 00:00:00 2001 From: "xueman.ruan" Date: Tue, 14 Feb 2023 10:03:23 +0800 Subject: [PATCH] [hal_jpege]: Add dma heap options for JPEG encode. use cachable dma buffer to reduce copy time. Signed-off-by: xueman.ruan Change-Id: I7fcc0fa5942ea5aa4e247b5f10677843d006ff28 --- inc/mpp_buffer.h | 7 +++- mpp/base/mpp_buffer_impl.cpp | 44 +++++++++++++++++++++++--- mpp/hal/vpu/jpege/hal_jpege_vepu2_v2.c | 7 ++-- osal/allocator/allocator_dma_heap.c | 5 +-- osal/allocator/allocator_drm.c | 1 + osal/allocator/allocator_ext_dma.c | 1 + osal/allocator/allocator_ion.c | 1 + osal/allocator/allocator_std.c | 1 + osal/inc/mpp_allocator.h | 1 + osal/mpp_allocator.cpp | 7 ++++ osal/mpp_runtime.cpp | 2 +- osal/os_allocator.h | 2 ++ 12 files changed, 68 insertions(+), 11 deletions(-) diff --git a/inc/mpp_buffer.h b/inc/mpp_buffer.h index 110a13ff..652acc87 100644 --- a/inc/mpp_buffer.h +++ b/inc/mpp_buffer.h @@ -142,9 +142,14 @@ typedef enum { * DRM SECURE buffer: MPP_BUFFER_TYPE_DRM | MPP_BUFFER_FLAGS_SECURE * = 0x00080003 * + * The dma buffer source can also be set by format: flags | type. + * dma buffer source flags: + * MPP_BUFFER_FLAGS_CONTIG means cma + * MPP_BUFFER_FLAGS_CACHABLE means cachable + * MPP_BUFFER_FLAGS_DMA32 means dma32 + * * flags originate from drm_rockchip_gem_mem_type */ - #define MPP_BUFFER_FLAGS_MASK 0x003f0000 //ROCKCHIP_BO_MASK << 16 #define MPP_BUFFER_FLAGS_CONTIG 0x00010000 //ROCKCHIP_BO_CONTIG << 16 #define MPP_BUFFER_FLAGS_CACHABLE 0x00020000 //ROCKCHIP_BO_CACHABLE << 16 diff --git a/mpp/base/mpp_buffer_impl.cpp b/mpp/base/mpp_buffer_impl.cpp index 89e59e99..3264227d 100644 --- a/mpp/base/mpp_buffer_impl.cpp +++ b/mpp/base/mpp_buffer_impl.cpp @@ -29,6 +29,7 @@ #define MAX_GROUP_BIT 8 #define MAX_MISC_GROUP_BIT 3 #define BUFFER_OPS_MAX_COUNT 1024 +#define MPP_BUFFER_SPECIAL_DMA_HEAP_NUM 8 #define SEARCH_GROUP_BY_ID(id) ((MppBufferService::get_instance())->get_group_by_id(id)) @@ -63,6 +64,8 @@ private: /* preset allocator apis */ MppAllocator mAllocator[MPP_BUFFER_TYPE_BUTT]; MppAllocatorApi *mAllocatorApi[MPP_BUFFER_TYPE_BUTT]; + /* special dma heap allocator apis */ + MppAllocator mAllocatorDmaHeapWithFlag[MPP_BUFFER_SPECIAL_DMA_HEAP_NUM]; struct list_head mListGroup; DECLARE_HASHTABLE(mHashGroup, MAX_GROUP_BIT); @@ -777,9 +780,6 @@ MppBufferService::MppBufferService() for (i = 0; i < (RK_S32)HASH_SIZE(mHashGroup); i++) INIT_HLIST_HEAD(&mHashGroup[i]); - - for (i = 0; i < MPP_BUFFER_TYPE_BUTT; i++) - mpp_allocator_get(&mAllocator[i], &mAllocatorApi[i], (MppBufferType)i); } #include "mpp_time.h" @@ -833,6 +833,9 @@ MppBufferService::~MppBufferService() for (i = 0; i < MPP_BUFFER_TYPE_BUTT; i++) mpp_allocator_put(&mAllocator[i]); + + for (i = 1; i < MPP_BUFFER_SPECIAL_DMA_HEAP_NUM; i++) + mpp_allocator_put(&mAllocatorDmaHeapWithFlag[i]); } RK_U32 MppBufferService::get_group_id() @@ -868,6 +871,7 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal RK_U32 is_misc) { MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK); + RK_U32 flags = (type & MPP_BUFFER_FLAGS_MASK); MppBufferGroupImpl *p = (MppBufferGroupImpl *)mpp_mem_pool_get_f(caller, mpp_buf_grp_pool); if (NULL == p) { mpp_err("MppBufferService failed to allocate group context\n"); @@ -896,8 +900,38 @@ MppBufferGroupImpl *MppBufferService::get_group(const char *tag, const char *cal pthread_mutex_init(&p->buf_lock, &attr); pthread_mutexattr_destroy(&attr); - p->allocator = mAllocator[type]; - p->alloc_api = mAllocatorApi[type]; + { + AutoMutex auto_lock(get_lock()); + + // allocate general buffer first + if (!mAllocator[buffer_type]) + mpp_allocator_get(&mAllocator[buffer_type], &mAllocatorApi[buffer_type], buffer_type); + + p->allocator = mAllocator[buffer_type]; + p->alloc_api = mAllocatorApi[buffer_type]; + + // allocate extra dma heap buffer if necessary + if (flags && MPP_BUFFER_TYPE_DMA_HEAP == get_real_allocator_type(p->allocator)) { + RK_U32 extra_allocator_idx = 0; + + // calculate index of extra allocator + if (flags & MPP_BUFFER_FLAGS_CONTIG) + extra_allocator_idx |= 1 << 0; + + if (flags & MPP_BUFFER_FLAGS_CACHABLE) + extra_allocator_idx |= 1 << 1; + + if (flags & MPP_BUFFER_FLAGS_DMA32) + extra_allocator_idx |= 1 << 2; + + if (!mAllocatorDmaHeapWithFlag[extra_allocator_idx]) + mpp_allocator_get(&mAllocatorDmaHeapWithFlag[extra_allocator_idx], + &p->alloc_api, type); + + if (mAllocatorDmaHeapWithFlag[extra_allocator_idx]) + p->allocator = mAllocatorDmaHeapWithFlag[extra_allocator_idx]; + } + } mpp_assert(p->allocator); mpp_assert(p->alloc_api); diff --git a/mpp/hal/vpu/jpege/hal_jpege_vepu2_v2.c b/mpp/hal/vpu/jpege/hal_jpege_vepu2_v2.c index 3ad2d979..b1febeb6 100644 --- a/mpp/hal/vpu/jpege/hal_jpege_vepu2_v2.c +++ b/mpp/hal/vpu/jpege/hal_jpege_vepu2_v2.c @@ -208,8 +208,11 @@ MPP_RET hal_jpege_vepu2_get_task(void *hal, HalEncTask *task) mpp_assert(ctx_ext); - if (!ctx_ext->partions_group) - mpp_buffer_group_get_internal(&ctx_ext->partions_group, MPP_BUFFER_TYPE_ION); + if (!ctx_ext->partions_group) { + mpp_buffer_group_get_internal(&ctx_ext->partions_group, MPP_BUFFER_TYPE_DMA_HEAP | MPP_BUFFER_FLAGS_CACHABLE); + if (!ctx_ext->partions_group) + mpp_buffer_group_get_internal(&ctx_ext->partions_group, MPP_BUFFER_TYPE_ION); + } mpp_assert(ctx_ext->partions_group); diff --git a/osal/allocator/allocator_dma_heap.c b/osal/allocator/allocator_dma_heap.c index 7e3a6371..b981d449 100644 --- a/osal/allocator/allocator_dma_heap.c +++ b/osal/allocator/allocator_dma_heap.c @@ -100,7 +100,7 @@ static int dma_heap_alloc(int fd, size_t len, RK_S32 *dmabuf_fd, RK_U32 flags) memset(&data, 0, sizeof(data)); data.len = len; data.fd_flags = O_RDWR | O_CLOEXEC; - data.heap_flags = flags; + data.heap_flags = 0; // heap_flags should be set to 0 ret = ioctl(fd, DMA_HEAP_IOCTL_ALLOC, &data); @@ -132,7 +132,7 @@ static int heap_fd_open(DmaHeapType type) int fd; snprintf(name, sizeof(name) - 1, "%s%s", heap_path, heap_names[type]); - fd = open(name, O_RDWR | O_CLOEXEC); + fd = open(name, O_RDONLY | O_CLOEXEC); // read authority is enough mpp_assert(fd > 0); dma_heap_dbg(DMA_HEAP_DEVICE, "open dma heap dev %s fd %d\n", name, fd); @@ -320,6 +320,7 @@ static MPP_RET os_allocator_dma_heap_mmap(void *ctx, MppBufferInfo *data) } os_allocator allocator_dma_heap = { + .type = MPP_BUFFER_TYPE_DMA_HEAP, .open = os_allocator_dma_heap_open, .close = os_allocator_dma_heap_close, .alloc = os_allocator_dma_heap_alloc, diff --git a/osal/allocator/allocator_drm.c b/osal/allocator/allocator_drm.c index c0bb9f25..3f5e04b2 100644 --- a/osal/allocator/allocator_drm.c +++ b/osal/allocator/allocator_drm.c @@ -322,6 +322,7 @@ static MPP_RET os_allocator_drm_mmap(void *ctx, MppBufferInfo *data) } os_allocator allocator_drm = { + .type = MPP_BUFFER_TYPE_DRM, .open = os_allocator_drm_open, .close = os_allocator_drm_close, .alloc = os_allocator_drm_alloc, diff --git a/osal/allocator/allocator_ext_dma.c b/osal/allocator/allocator_ext_dma.c index 92223b6e..0c7163f8 100644 --- a/osal/allocator/allocator_ext_dma.c +++ b/osal/allocator/allocator_ext_dma.c @@ -135,6 +135,7 @@ static MPP_RET allocator_ext_dma_close(void *ctx) } os_allocator allocator_ext_dma = { + .type = MPP_BUFFER_TYPE_EXT_DMA, .open = allocator_ext_dma_open, .close = allocator_ext_dma_close, .alloc = allocator_ext_dma_alloc, diff --git a/osal/allocator/allocator_ion.c b/osal/allocator/allocator_ion.c index b6d3b6e2..aa7f871c 100644 --- a/osal/allocator/allocator_ion.c +++ b/osal/allocator/allocator_ion.c @@ -482,6 +482,7 @@ static MPP_RET allocator_ion_close(void *ctx) } os_allocator allocator_ion = { + .type = MPP_BUFFER_TYPE_ION, .open = allocator_ion_open, .close = allocator_ion_close, .alloc = allocator_ion_alloc, diff --git a/osal/allocator/allocator_std.c b/osal/allocator/allocator_std.c index d703fafa..30451c36 100644 --- a/osal/allocator/allocator_std.c +++ b/osal/allocator/allocator_std.c @@ -114,6 +114,7 @@ static MPP_RET allocator_std_close(void *ctx) } os_allocator allocator_std = { + .type = MPP_BUFFER_TYPE_NORMAL, .open = allocator_std_open, .close = allocator_std_close, .alloc = allocator_std_alloc, diff --git a/osal/inc/mpp_allocator.h b/osal/inc/mpp_allocator.h index 18643f09..4e1c1e1f 100644 --- a/osal/inc/mpp_allocator.h +++ b/osal/inc/mpp_allocator.h @@ -46,6 +46,7 @@ extern "C" { MPP_RET mpp_allocator_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type); MPP_RET mpp_allocator_put(MppAllocator *allocator); +MppBufferType get_real_allocator_type(const MppAllocator allocator); #ifdef __cplusplus } diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index 25277fc5..3647fb22 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -189,3 +189,10 @@ MPP_RET mpp_allocator_put(MppAllocator *allocator) return MPP_OK; } +MppBufferType get_real_allocator_type(const MppAllocator allocator) +{ + MppAllocatorImpl *p = (MppAllocatorImpl *)allocator; + MppBufferType type = allocator ? p->os_api.type : MPP_BUFFER_TYPE_BUTT; + + return type; +} \ No newline at end of file diff --git a/osal/mpp_runtime.cpp b/osal/mpp_runtime.cpp index 3b40c4f8..c2379963 100644 --- a/osal/mpp_runtime.cpp +++ b/osal/mpp_runtime.cpp @@ -86,7 +86,7 @@ MppRuntimeService::MppRuntimeService() allocator_valid[MPP_BUFFER_TYPE_NORMAL] = 1; allocator_valid[MPP_BUFFER_TYPE_ION] = !access("/dev/ion", F_OK | R_OK | W_OK); allocator_valid[MPP_BUFFER_TYPE_DRM] = !access("/dev/dri/card0", F_OK | R_OK | W_OK); - allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP] = !access("/dev/dma_heap", F_OK | R_OK | W_OK); + allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP] = !access("/dev/dma_heap", F_OK | R_OK); if (!allocator_valid[MPP_BUFFER_TYPE_ION] && !allocator_valid[MPP_BUFFER_TYPE_DRM] && diff --git a/osal/os_allocator.h b/osal/os_allocator.h index d19098b4..0ffa8fc5 100644 --- a/osal/os_allocator.h +++ b/osal/os_allocator.h @@ -22,6 +22,8 @@ typedef MPP_RET (*OsAllocatorFunc)(void *ctx, MppBufferInfo *info); typedef struct os_allocator_t { + MppBufferType type; + MPP_RET (*open)(void **ctx, MppAllocatorCfg *cfg); MPP_RET (*close)(void *ctx);