• 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         *pos->comp = NULL;
149         DListRemove(&pos->node);
150         OsalMemFree(pos);
151         pos = NULL;
152         break;
153     }
154     pthread_mutex_unlock(&g_list->listMute);
155 
156     return ret;
157 }
158 
InitComponentConfig(void)159 static int32_t InitComponentConfig(void)
160 {
161     ReleaseHcsTree();
162     const struct DeviceResourceIface *pDevResIns = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
163     if (pDevResIns == NULL) {
164         HDF_LOGE("get hcs interface failed.");
165         return HDF_FAILURE;
166     }
167 
168     SetHcsBlobPath(CONFIG_PATH_NAME);
169     const struct DeviceResourceNode *pRootNode = pDevResIns->GetRootNode();
170     if (pRootNode == NULL) {
171         HDF_LOGE("GetRootNode failed");
172         return HDF_FAILURE;
173     }
174 
175     const struct DeviceResourceNode *codecConfig = pDevResIns->GetChildNode(pRootNode, "codec_adapter_config");
176     if (codecConfig == NULL) {
177         HDF_LOGE("codecConfig failed");
178         return HDF_FAILURE;
179     }
180 
181     InitDataNode(codecConfig);
182     if (LoadCapabilityData() != HDF_SUCCESS) {
183         ClearCapabilityData();
184     }
185     if (LoadExInfoData(codecConfig) != HDF_SUCCESS) {
186         ClearExInfoData();
187     }
188     return HDF_SUCCESS;
189 }
190 
191 static struct CodecComponentManager g_codecComponentManager = {
192     .GetComponentNum = ComponentManagerGetComponentNum,
193     .GetComponentCapabilityList = ComponentManagerGetComponentCapabilityList,
194     .CreateComponent = ComponentManagerCreateComponent,
195     .DestroyComponent = ComponentManagerDestoryComponent,
196 };
197 
GetCodecComponentManager(void)198 struct CodecComponentManager *GetCodecComponentManager(void)
199 {
200     if (g_list == NULL) {
201         g_list = (struct ComponentManagerList *)OsalMemCalloc(sizeof(struct ComponentManagerList));
202         if (g_list == NULL) {
203             HDF_LOGE("%{public}s: malloc ComponentManagerList obj failed!", __func__);
204             return NULL;
205         }
206         DListHeadInit(&g_list->head);
207     }
208     if (CodecAdapterCodecInit() != HDF_SUCCESS) {
209         HDF_LOGE("%{public}s CodecAdapterCodecInit error", __func__);
210         return NULL;
211     }
212     if (InitComponentConfig() != HDF_SUCCESS) {
213         CodecAdapterCodecDeinit();
214         HDF_LOGE("%{public}s InitComponentConfig error", __func__);
215         return NULL;
216     }
217     return &g_codecComponentManager;
218 }
219 
CodecComponentManagerRelease(void)220 void CodecComponentManagerRelease(void)
221 {
222     if (ClearCapabilityData() != HDF_SUCCESS) {
223         HDF_LOGE("%{public}s ClearCapabilityData failed !", __func__);
224     }
225     if (ClearExInfoData() != HDF_SUCCESS) {
226         HDF_LOGE("%{public}s ClearExInfoData failed !", __func__);
227     }
228     if (CodecAdapterCodecDeinit() != HDF_SUCCESS) {
229         HDF_LOGE("%{public}s CodecAdapterCodecDeinit error", __func__);
230         return;
231     }
232     if (g_list != NULL) {
233         OsalMemFree(g_list);
234         g_list = NULL;
235     }
236 
237     HDF_LOGI("%{public}s end", __func__);
238 }
239