mirror of
https://github.com/nyanmisaka/mpp.git
synced 2025-10-05 09:06:50 +08:00
refactor[allocator]: Refactor allocator flow
1. Allocator api is determined by buffer type. 2. Allocator ctx is determined by both buffer type and buffer flags. 3. All allocator dma_heap / drm / ion / ext_dma should support different buffer flags. 4. The buffer flag supports cacheable / 32-bit / contig options. 5. Add flags update flow on allocator create process. 6. Add mpp_dmabuf_has_partial_ops to disable dmabuf partial ops when the kernel driver has bug. Signed-off-by: Herman Chen <herman.chen@rock-chips.com> Change-Id: If36a05c6704112cad3ab46861023597ff02bf14c
This commit is contained in:
@@ -19,10 +19,15 @@
|
||||
#include "mpp_mem.h"
|
||||
#include "mpp_debug.h"
|
||||
#include "mpp_common.h"
|
||||
#include "mpp_runtime.h"
|
||||
#include "mpp_thread.h"
|
||||
#include "mpp_allocator.h"
|
||||
#include "mpp_allocator_impl.h"
|
||||
|
||||
#include "os_allocator.h"
|
||||
#include "allocator_std.h"
|
||||
#include "allocator_ion.h"
|
||||
#include "allocator_drm.h"
|
||||
#include "allocator_ext_dma.h"
|
||||
#include "allocator_dma_heap.h"
|
||||
|
||||
#include <linux/drm.h>
|
||||
|
||||
@@ -38,6 +43,15 @@ typedef enum OsAllocatorApiId_e {
|
||||
ALLOC_API_BUTT,
|
||||
} OsAllocatorApiId;
|
||||
|
||||
typedef struct MppAllocatorImpl_t {
|
||||
pthread_mutex_t lock;
|
||||
MppBufferType type;
|
||||
MppAllocFlagType flags;
|
||||
size_t alignment;
|
||||
os_allocator os_api;
|
||||
void *ctx;
|
||||
} MppAllocatorImpl;
|
||||
|
||||
static MPP_RET mpp_allocator_api_wrapper(MppAllocator allocator,
|
||||
MppBufferInfo *info,
|
||||
OsAllocatorApiId id)
|
||||
@@ -118,56 +132,83 @@ static MppAllocatorApi mpp_allocator_api = {
|
||||
.mmap = mpp_allocator_mmap,
|
||||
};
|
||||
|
||||
MPP_RET mpp_allocator_get(MppAllocator *allocator,
|
||||
MppAllocatorApi **api, MppBufferType type)
|
||||
MPP_RET mpp_allocator_get(MppAllocator *allocator, MppAllocatorApi **api,
|
||||
MppBufferType type, MppAllocFlagType flags)
|
||||
{
|
||||
MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
|
||||
RK_U32 flags = (type & MPP_BUFFER_FLAGS_MASK) >> 16;
|
||||
MppAllocatorImpl *p = NULL;
|
||||
|
||||
if (NULL == allocator || NULL == api || buffer_type >= MPP_BUFFER_TYPE_BUTT) {
|
||||
mpp_err_f("invalid input: allocator %p api %p type %d\n",
|
||||
allocator, api, buffer_type);
|
||||
return MPP_ERR_UNKNOW;
|
||||
}
|
||||
do {
|
||||
if (NULL == allocator || NULL == api || buffer_type >= MPP_BUFFER_TYPE_BUTT) {
|
||||
mpp_err_f("invalid input: allocator %p api %p type %d\n",
|
||||
allocator, api, buffer_type);
|
||||
break;
|
||||
}
|
||||
|
||||
p = mpp_malloc(MppAllocatorImpl, 1);
|
||||
if (!p) {
|
||||
mpp_err_f("failed to malloc allocator context\n");
|
||||
break;
|
||||
}
|
||||
|
||||
MppAllocatorImpl *p = mpp_malloc(MppAllocatorImpl, 1);
|
||||
if (NULL == p) {
|
||||
mpp_err("mpp_allocator_get failed to malloc allocator context\n");
|
||||
return MPP_ERR_NULL_PTR;
|
||||
} else {
|
||||
p->type = buffer_type;
|
||||
p->flags = flags;
|
||||
}
|
||||
|
||||
MPP_RET ret = os_allocator_get(&p->os_api, buffer_type);
|
||||
switch (buffer_type) {
|
||||
case MPP_BUFFER_TYPE_NORMAL : {
|
||||
p->os_api = allocator_std;
|
||||
} break;
|
||||
case MPP_BUFFER_TYPE_ION : {
|
||||
p->os_api = (mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_DMA_HEAP)) ? allocator_dma_heap :
|
||||
(mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_ION)) ? allocator_ion :
|
||||
(mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_DRM)) ? allocator_drm :
|
||||
allocator_std;
|
||||
} break;
|
||||
case MPP_BUFFER_TYPE_EXT_DMA: {
|
||||
p->os_api = allocator_ext_dma;
|
||||
} break;
|
||||
case MPP_BUFFER_TYPE_DRM : {
|
||||
p->os_api = (mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_DMA_HEAP)) ? allocator_dma_heap :
|
||||
(mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_DRM)) ? allocator_drm :
|
||||
(mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_ION)) ? allocator_ion :
|
||||
allocator_std;
|
||||
} break;
|
||||
case MPP_BUFFER_TYPE_DMA_HEAP: {
|
||||
p->os_api = (mpp_rt_allcator_is_valid(MPP_BUFFER_TYPE_DMA_HEAP)) ? allocator_dma_heap :
|
||||
allocator_std;
|
||||
} break;
|
||||
default : {
|
||||
} break;
|
||||
}
|
||||
|
||||
if (MPP_OK == ret) {
|
||||
MppAllocatorCfg cfg = {
|
||||
.alignment = SZ_4K,
|
||||
.flags = flags,
|
||||
};
|
||||
ret = p->os_api.open(&p->ctx, &cfg);
|
||||
if (p->os_api.open(&p->ctx, SZ_4K, flags))
|
||||
break;
|
||||
|
||||
/* NOTE: allocator may update the flags for compatibility */
|
||||
p->flags = cfg.flags;
|
||||
}
|
||||
if (MPP_OK == ret) {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&p->lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
/* update the real buffer type and flags */
|
||||
p->type = p->os_api.type;
|
||||
if (p->os_api.flags)
|
||||
p->flags = p->os_api.flags(p->ctx);
|
||||
|
||||
*allocator = p;
|
||||
*api = &mpp_allocator_api;
|
||||
} else {
|
||||
mpp_err("mpp_allocator_get type %d failed\n", type);
|
||||
mpp_free(p);
|
||||
*allocator = NULL;
|
||||
*api = NULL;
|
||||
}
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
|
||||
return ret;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&p->lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
|
||||
*allocator = p;
|
||||
*api = &mpp_allocator_api;
|
||||
|
||||
return MPP_OK;
|
||||
} while (0);
|
||||
|
||||
MPP_FREE(p);
|
||||
*allocator = NULL;
|
||||
*api = NULL;
|
||||
|
||||
return MPP_NOK;
|
||||
}
|
||||
|
||||
MPP_RET mpp_allocator_put(MppAllocator *allocator)
|
||||
@@ -191,10 +232,9 @@ MPP_RET mpp_allocator_put(MppAllocator *allocator)
|
||||
return MPP_OK;
|
||||
}
|
||||
|
||||
MppBufferType get_real_allocator_type(const MppAllocator allocator)
|
||||
MppAllocFlagType mpp_allocator_get_flags(const MppAllocator allocator)
|
||||
{
|
||||
MppAllocatorImpl *p = (MppAllocatorImpl *)allocator;
|
||||
MppBufferType type = allocator ? p->os_api.type : MPP_BUFFER_TYPE_BUTT;
|
||||
|
||||
return type;
|
||||
}
|
||||
return p ? p->flags : MPP_ALLOC_FLAG_NONE;
|
||||
}
|
||||
|
Reference in New Issue
Block a user