• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "QComOMXPlugin.h"
18 
19 #include <dlfcn.h>
20 
21 #include <HardwareAPI.h>
22 
23 namespace android {
24 
25 static const char kPrefix[] = "7x30.";
26 
AddPrefix(char * name)27 static OMX_ERRORTYPE AddPrefix(char *name) {
28     if (strncmp("OMX.qcom.", name, 9)) {
29         return OMX_ErrorUndefined;
30     }
31     String8 tmp(name, 9);
32     tmp.append(kPrefix);
33     tmp.append(&name[9]);
34     strcpy(name, tmp.string());
35     return OMX_ErrorNone;
36 }
37 
RemovePrefix(const char * name,String8 * out)38 static void RemovePrefix(const char *name, String8 *out) {
39     out->setTo(name, 9);  // "OMX.qcom."
40     out->append(&name[9 + strlen(kPrefix)]);
41 }
42 
createOMXPlugin()43 OMXPluginBase *createOMXPlugin() {
44     return new QComOMXPlugin;
45 }
46 
QComOMXPlugin()47 QComOMXPlugin::QComOMXPlugin()
48     : mLibHandle(dlopen("libOmxCore.so", RTLD_NOW)),
49       mInit(NULL),
50       mDeinit(NULL),
51       mComponentNameEnum(NULL),
52       mGetHandle(NULL),
53       mFreeHandle(NULL),
54       mGetRolesOfComponentHandle(NULL) {
55     if (mLibHandle != NULL) {
56         mInit = (InitFunc)dlsym(mLibHandle, "OMX_Init");
57         mDeinit = (DeinitFunc)dlsym(mLibHandle, "OMX_DeInit");
58 
59         mComponentNameEnum =
60             (ComponentNameEnumFunc)dlsym(mLibHandle, "OMX_ComponentNameEnum");
61 
62         mGetHandle = (GetHandleFunc)dlsym(mLibHandle, "OMX_GetHandle");
63         mFreeHandle = (FreeHandleFunc)dlsym(mLibHandle, "OMX_FreeHandle");
64 
65         mGetRolesOfComponentHandle =
66             (GetRolesOfComponentFunc)dlsym(
67                     mLibHandle, "OMX_GetRolesOfComponent");
68 
69         (*mInit)();
70     }
71 }
72 
~QComOMXPlugin()73 QComOMXPlugin::~QComOMXPlugin() {
74     if (mLibHandle != NULL) {
75         (*mDeinit)();
76 
77         dlclose(mLibHandle);
78         mLibHandle = NULL;
79     }
80 }
81 
makeComponentInstance(const char * name,const OMX_CALLBACKTYPE * callbacks,OMX_PTR appData,OMX_COMPONENTTYPE ** component)82 OMX_ERRORTYPE QComOMXPlugin::makeComponentInstance(
83         const char *name,
84         const OMX_CALLBACKTYPE *callbacks,
85         OMX_PTR appData,
86         OMX_COMPONENTTYPE **component) {
87     if (mLibHandle == NULL) {
88         return OMX_ErrorUndefined;
89     }
90 
91     String8 tmp;
92     RemovePrefix(name, &tmp);
93     name = tmp.string();
94 
95     return (*mGetHandle)(
96             reinterpret_cast<OMX_HANDLETYPE *>(component),
97             const_cast<char *>(name),
98             appData, const_cast<OMX_CALLBACKTYPE *>(callbacks));
99 }
100 
destroyComponentInstance(OMX_COMPONENTTYPE * component)101 OMX_ERRORTYPE QComOMXPlugin::destroyComponentInstance(
102         OMX_COMPONENTTYPE *component) {
103     if (mLibHandle == NULL) {
104         return OMX_ErrorUndefined;
105     }
106 
107     return (*mFreeHandle)(reinterpret_cast<OMX_HANDLETYPE *>(component));
108 }
109 
enumerateComponents(OMX_STRING name,size_t size,OMX_U32 index)110 OMX_ERRORTYPE QComOMXPlugin::enumerateComponents(
111         OMX_STRING name,
112         size_t size,
113         OMX_U32 index) {
114     if (mLibHandle == NULL) {
115         return OMX_ErrorUndefined;
116     }
117 
118     OMX_ERRORTYPE res = (*mComponentNameEnum)(name, size, index);
119 
120     if (res != OMX_ErrorNone) {
121         return res;
122     }
123 
124     return AddPrefix(name);
125 }
126 
getRolesOfComponent(const char * name,Vector<String8> * roles)127 OMX_ERRORTYPE QComOMXPlugin::getRolesOfComponent(
128         const char *name,
129         Vector<String8> *roles) {
130     roles->clear();
131 
132     if (mLibHandle == NULL) {
133         return OMX_ErrorUndefined;
134     }
135 
136     String8 tmp;
137     RemovePrefix(name, &tmp);
138     name = tmp.string();
139 
140     OMX_U32 numRoles;
141     OMX_ERRORTYPE err = (*mGetRolesOfComponentHandle)(
142             const_cast<OMX_STRING>(name), &numRoles, NULL);
143 
144     if (err != OMX_ErrorNone) {
145         return err;
146     }
147 
148     if (numRoles > 0) {
149         OMX_U8 **array = new OMX_U8 *[numRoles];
150         for (OMX_U32 i = 0; i < numRoles; ++i) {
151             array[i] = new OMX_U8[OMX_MAX_STRINGNAME_SIZE];
152         }
153 
154         OMX_U32 numRoles2;
155         err = (*mGetRolesOfComponentHandle)(
156                 const_cast<OMX_STRING>(name), &numRoles2, array);
157 
158         if (err == OMX_ErrorNone && numRoles != numRoles2) {
159             err = OMX_ErrorUndefined;
160         }
161 
162         for (OMX_U32 i = 0; i < numRoles; ++i) {
163             if (err == OMX_ErrorNone) {
164                 String8 s((const char *)array[i]);
165                 roles->push(s);
166             }
167 
168             delete[] array[i];
169             array[i] = NULL;
170         }
171 
172         delete[] array;
173         array = NULL;
174     }
175 
176     return err;
177 }
178 
179 }  // namespace android
180