• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "codec_component_manager.h"
17 #include <hdf_dlist.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <osal_mem.h>
21 #include "hcs_dm_parser.h"
22 #include "codec_adapter_if.h"
23 #include "codec_component_capability_config.h"
24 #include "codec_component_capability.h"
25 
26 #define HDF_LOG_TAG codec_hdi_passthrough
27 
28 #define CONFIG_PATH_NAME HDF_CONFIG_DIR"/codec_adapter_capabilities.hcb"
29 
30 struct ComponentManagerList *g_list = NULL;
31 uint32_t g_componentId = 0;
GetNextComponentId()32 static uint32_t GetNextComponentId()
33 {
34     uint32_t tempId = 0;
35     if (g_list == NULL) {
36         return tempId;
37     }
38     struct ComponentIdElement *pos = NULL;
39     bool find = false;
40 
41     do {
42         tempId = ++g_componentId;
43         find = false;
44         DLIST_FOR_EACH_ENTRY(pos, &g_list->head, struct ComponentIdElement, node) {
45             if (pos != NULL && tempId == pos->componentId) {
46                 find = true;
47                 break;
48             }
49         }
50     } while (find);
51     return tempId;
52 }
53 
ComponentManagerGetComponentNum()54 static int32_t ComponentManagerGetComponentNum()
55 {
56     int32_t num = 0;
57     if (GetComponentNum(&num) != HDF_SUCCESS) {
58         HDF_LOGE("%{public}s, GetComponentNum error!", __func__);
59     }
60     return num;
61 }
62 
ComponentManagerGetComponentCapabilityList(CodecCompCapability * capList,int32_t count)63 static int32_t ComponentManagerGetComponentCapabilityList(CodecCompCapability *capList, int32_t count)
64 {
65     if (capList == NULL || count <= 0) {
66         HDF_LOGE("%{public}s, capList is null or count[%{public}d] <= 0!", __func__, count);
67         return HDF_ERR_INVALID_PARAM;
68     }
69     int32_t ret = GetComponentCapabilityList(capList, count);
70     if (ret != HDF_SUCCESS) {
71         HDF_LOGE("%{public}s, GetComponentCapabilityList error!", __func__);
72     }
73     return ret;
74 }
75 
ComponentManagerCreateComponent(struct CodecComponentType ** component,uint32_t * componentId,char * compName,int64_t appData,struct CodecCallbackType * callbacks)76 static int32_t ComponentManagerCreateComponent(struct CodecComponentType **component, uint32_t *componentId,
77                                                char *compName, int64_t appData, struct CodecCallbackType *callbacks)
78 {
79     if (g_list == NULL) {
80         HDF_LOGE("%{public}s, g_list is not init!", __func__);
81         return HDF_ERR_INVALID_PARAM;
82     }
83     struct ComponentIdElement *node = (struct ComponentIdElement *)OsalMemCalloc(sizeof(struct ComponentIdElement));
84     if (node == NULL) {
85         HDF_LOGE("%{public}s, CodecComponentTypeServiceGet ret null!", __func__);
86         return HDF_ERR_INVALID_PARAM;
87     }
88     struct CodecComponentNode *codecNode = NULL;
89     int32_t ret = CodecAdapterCreateComponent(&codecNode, compName, appData, callbacks);
90     if (ret != HDF_SUCCESS) {
91         HDF_LOGE("%{public}s CodecAdapterCreateComponent error", __func__);
92         OsalMemFree(node);
93         return ret;
94     }
95 
96     *component = CodecComponentTypeGet(NULL);
97     if (*component == NULL) {
98         HDF_LOGE("%{public}s: component is null", __func__);
99         CodecAdapterDestroyComponent(codecNode);
100         OsalMemFree(node);
101         return HDF_FAILURE;
102     }
103 
104     struct CodecComponentTypeInfo *info = CONTAINER_OF(*component, struct CodecComponentTypeInfo, instance);
105     if (info == NULL) {
106         HDF_LOGE("%{public}s: info is null", __func__);
107         CodecAdapterDestroyComponent(codecNode);
108         CodecComponentTypeRelease(*component);
109         OsalMemFree(node);
110         return HDF_FAILURE;
111     }
112     info->codecNode = codecNode;
113     pthread_mutex_lock(&g_list->listMute);
114     *componentId = GetNextComponentId();
115     DListInsertTail(&node->node, &g_list->head);
116     pthread_mutex_unlock(&g_list->listMute);
117     node->componentId = *componentId;
118     node->info = info;
119     node->comp = component;
120     return HDF_SUCCESS;
121 }
122 
ComponentManagerDestoryComponent(uint32_t componentId)123 static int32_t ComponentManagerDestoryComponent(uint32_t componentId)
124 {
125     if (g_list == NULL) {
126         HDF_LOGE("%{public}s, g_list is not init!", __func__);
127         return HDF_ERR_INVALID_PARAM;
128     }
129     struct ComponentIdElement *pos = NULL;
130     struct ComponentIdElement *next = NULL;
131     int32_t ret = HDF_FAILURE;
132     pthread_mutex_lock(&g_list->listMute);
133     DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &g_list->head, struct ComponentIdElement, node) {
134         if (pos == NULL || componentId != pos->componentId) {
135             continue;
136         }
137         if (pos->info == NULL) {
138             HDF_LOGE("%{public}s: info is null", __func__);
139             ret = HDF_FAILURE;
140             break;
141         }
142         ret = CodecAdapterDestroyComponent(pos->info->codecNode);
143         if (ret != HDF_SUCCESS) {
144             HDF_LOGE("%{public}s CodecAdapterDestroyComponent error", __func__);
145             break;
146         }
147         CodecComponentTypeRelease(&pos->info->instance);
148         if (pos->comp != NULL) {
149             *pos->comp = NULL;
150         }
151         DListRemove(&pos->node);
152         OsalMemFree(pos);
153         pos = NULL;
154         break;
155     }
156     pthread_mutex_unlock(&g_list->listMute);
157 
158     return ret;
159 }
160 
InitComponentConfig(void)161 static int32_t InitComponentConfig(void)
162 {
163     ReleaseHcsTree();
164     const struct DeviceResourceIface *pDevResIns = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
165     if (pDevResIns == NULL) {
166         HDF_LOGE("get hcs interface failed.");
167         return HDF_FAILURE;
168     }
169 
170     SetHcsBlobPath(CONFIG_PATH_NAME);
171     const struct DeviceResourceNode *pRootNode = pDevResIns->GetRootNode();
172     if (pRootNode == NULL) {
173         HDF_LOGE("GetRootNode failed");
174         return HDF_FAILURE;
175     }
176 
177     const struct DeviceResourceNode *codecConfig = pDevResIns->GetChildNode(pRootNode, "codec_adapter_config");
178     if (codecConfig == NULL) {
179         HDF_LOGE("codecConfig failed");
180         return HDF_FAILURE;
181     }
182 
183     InitDataNode(codecConfig);
184     if (LoadCapabilityData() != HDF_SUCCESS) {
185         ClearCapabilityData();
186     }
187     if (LoadExInfoData(codecConfig) != HDF_SUCCESS) {
188         ClearExInfoData();
189     }
190     return HDF_SUCCESS;
191 }
192 
193 static struct CodecComponentManager g_codecComponentManager = {
194     .GetComponentNum = ComponentManagerGetComponentNum,
195     .GetComponentCapabilityList = ComponentManagerGetComponentCapabilityList,
196     .CreateComponent = ComponentManagerCreateComponent,
197     .DestroyComponent = ComponentManagerDestoryComponent,
198 };
199 
GetCodecComponentManager(void)200 struct CodecComponentManager *GetCodecComponentManager(void)
201 {
202     if (g_list == NULL) {
203         g_list = (struct ComponentManagerList *)OsalMemCalloc(sizeof(struct ComponentManagerList));
204         if (g_list == NULL) {
205             HDF_LOGE("%{public}s: malloc ComponentManagerList obj failed!", __func__);
206             return NULL;
207         }
208         DListHeadInit(&g_list->head);
209     }
210     if (CodecAdapterCodecInit() != HDF_SUCCESS) {
211         HDF_LOGE("%{public}s CodecAdapterCodecInit error", __func__);
212         return NULL;
213     }
214     if (InitComponentConfig() != HDF_SUCCESS) {
215         CodecAdapterCodecDeinit();
216         HDF_LOGE("%{public}s InitComponentConfig error", __func__);
217         return NULL;
218     }
219     return &g_codecComponentManager;
220 }
221 
CodecComponentManagerRelease(void)222 void CodecComponentManagerRelease(void)
223 {
224     if (ClearCapabilityData() != HDF_SUCCESS) {
225         HDF_LOGE("%{public}s ClearCapabilityData failed !", __func__);
226     }
227     if (ClearExInfoData() != HDF_SUCCESS) {
228         HDF_LOGE("%{public}s ClearExInfoData failed !", __func__);
229     }
230     if (CodecAdapterCodecDeinit() != HDF_SUCCESS) {
231         HDF_LOGE("%{public}s CodecAdapterCodecDeinit error", __func__);
232         return;
233     }
234     if (g_list != NULL) {
235         OsalMemFree(g_list);
236         g_list = NULL;
237     }
238 
239     HDF_LOGI("%{public}s end", __func__);
240 }
241