hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * 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.
 */
 
#define MODULE_TAG "mpp_allocator"
 
#include "mpp_mem.h"
#include "mpp_debug.h"
#include "mpp_common.h"
#include "mpp_allocator.h"
#include "mpp_allocator_impl.h"
 
#include "os_allocator.h"
 
#include <linux/drm.h>
 
#define MPP_ALLOCATOR_LOCK(p)   pthread_mutex_lock(&(p)->lock);
#define MPP_ALLOCATOR_UNLOCK(p) pthread_mutex_unlock(&(p)->lock);
 
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 || 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);
    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;
}
 
static MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferInfo *info)
{
    return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_ALLOC);
}
 
static MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferInfo *info)
{
    return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_FREE);
}
 
static MPP_RET mpp_allocator_import(MppAllocator allocator, MppBufferInfo *info)
{
    return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_IMPORT);
}
 
static MPP_RET mpp_allocator_release(MppAllocator allocator,
                                     MppBufferInfo *info)
{
    return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_RELEASE);
}
 
static MPP_RET mpp_allocator_mmap(MppAllocator allocator, MppBufferInfo *info)
{
    return mpp_allocator_api_wrapper(allocator, info, ALLOC_API_MMAP);
}
 
static MppAllocatorApi mpp_allocator_api = {
    .size = sizeof(mpp_allocator_api),
    .version = 1,
    .alloc = mpp_allocator_alloc,
    .free = mpp_allocator_free,
    .import = mpp_allocator_import,
    .release =  mpp_allocator_release,
    .mmap  = mpp_allocator_mmap,
};
 
MPP_RET mpp_allocator_get(MppAllocator *allocator,
                          MppAllocatorApi **api, MppBufferType type)
{
    MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK);
    RK_U32 flags = (type & MPP_BUFFER_FLAGS_MASK) >> 16;
 
    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;
    }
 
    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);
 
    if (MPP_OK == ret) {
        MppAllocatorCfg cfg = {
            .alignment = SZ_4K,
            .flags = flags,
        };
        ret = p->os_api.open(&p->ctx, &cfg);
    }
    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);
 
        *allocator  = p;
        *api        = &mpp_allocator_api;
    } else {
        mpp_err("mpp_allocator_get type %d failed\n", type);
        mpp_free(p);
        *allocator  = NULL;
        *api        = NULL;
    }
 
    return ret;
}
 
MPP_RET mpp_allocator_put(MppAllocator *allocator)
{
    if (NULL == allocator) {
        mpp_err_f("invalid input: allocator %p\n", allocator);
        return MPP_ERR_NULL_PTR;
    }
 
    MppAllocatorImpl *p = (MppAllocatorImpl *)*allocator;
    *allocator = NULL;
    if (p->os_api.close && p->ctx)
        p->os_api.close(p->ctx);
    pthread_mutex_destroy(&p->lock);
    mpp_free(p);
 
    return MPP_OK;
}