1 /*
2 * Copyright (c) 2009-2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     cplib_utils.h
24 //! \brief    Utils to interact with CPLib
25 //!
26 #ifndef __CPLIB_UTILS_H__
27 #define __CPLIB_UTILS_H__
28 
29 #include "mos_util_debug.h"
30 
31 #include <dlfcn.h>
32 #include <unordered_map>
33 #include <mutex>
34 
35 #define REQUIRED_CPLIB_MAJOR_VERSION 1
36 
37 #define CPLIB_NORMALMESSAGE(_message, ...)                                    \
38 MOS_NORMALMESSAGE(MOS_COMPONENT_CPLIB, MOS_CP_SUBCOMP_CPLIB, _message, ##__VA_ARGS__)
39 
40 #define CPLIB_ASSERTMESSAGE(_message, ...)                                    \
41 MOS_ASSERTMESSAGE(MOS_COMPONENT_CPLIB, MOS_CP_SUBCOMP_CPLIB, _message, ##__VA_ARGS__)
42 
43 typedef struct VADriverContext* VADriverContextP;
44 
45 class CPLibUtils
46 {
47 public:
48     CPLibUtils() = delete;
49     ~CPLibUtils() = delete;
50     CPLibUtils(const CPLibUtils& ) = delete;
51     CPLibUtils & operator=(const CPLibUtils& other) = delete;
52 
53     //!
54     //! \brief    Try load CPLIB
55     //!
56     static bool LoadCPLib(VADriverContextP ctx);
57 
58     //!
59     //! \brief    Unload CPLIB if it is loaded
60     //!
61     static void UnloadCPLib(VADriverContextP ctx);
62 
63     //!
64     //! \brief    Invoke CPLIB function
65     //! \param    [out] _ret
66     //!           To store the return of involved function
67     //!
68     //! \param    [in] *symbol
69     //!           The symbol want to involve in CPLIB
70     //!
71     //! \param    [in] args
72     //!           All the args needed for the function you involved in CPLIB
73     //!
74     template<typename _FuncType, typename _RetType, typename... Args>
75     static void InvokeCpFunc(_RetType& _ret, const char * symbol, Args... args);
76 
77     //!
78     //! \brief    Invoke CPLIB function
79     //!
80     //! \param    [in] *symbol
81     //!           The symbol want to involve in CPLIB
82     //!
83     //! \param    [in] args
84     //!           All the args needed for the function you involved in CPLIB
85     //!
86     template<typename _FuncType, typename... Args>
87     static void InvokeCpFunc(const char * symbol, Args... args);
88 
89     static const char* CPLIB_PATH;
90     static const char* FUNC_GET_CPLIB_MAJOR_VERSION;
91     static const char* FUNC_INIT_CPLIB;
92     static const char* FUNC_RELEASE_CPLIB;
93     static const char* FUNC_CREATE_DDICP;
94     static const char* FUNC_DELETE_DDICP;
95     static const char* FUNC_CREATE_MHWCP;
96     static const char* FUNC_DELETE_MHWCP;
97     static const char* FUNC_CREATE_MOSCP;
98     static const char* FUNC_DELETE_MOSCP;
99     static const char* FUNC_CREATE_MEDIALIBVACAPSCP;
100     static const char* FUNC_DELETE_MEDIALIBVACAPSCP;
101     static const char* FUNC_CREATE_SECUREDECODE;
102     static const char* FUNC_DELETE_SECUREDECODE;
103     static const char* FUNC_CREATE_DECODECP;
104     static const char* FUNC_DELETE_DECODECP;
105     static const char* FUNC_CREATE_CPSTREAMOUT;
106     static const char* FUNC_DELETE_CPSTREAMOUT;
107 
108     static std::unordered_map<const char*, void*> m_symbols;
109     static std::mutex                             m_referenceMutex;
110     static int                                    m_referenceCount;
111 
112 private:
113     static void* m_phandle;
114 };
115 
116 template<typename _FuncType, typename _RetType, typename... Args>
InvokeCpFunc(_RetType & _ret,const char * symbol,Args...args)117 void CPLibUtils::InvokeCpFunc(_RetType& _ret, const char* symbol, Args... args)
118 {
119     if (m_symbols.end() != m_symbols.find(symbol))
120     {
121         _FuncType func = reinterpret_cast<_FuncType>(m_symbols[symbol]);
122         if (nullptr != func)
123         {
124             _ret = func(args...);
125             return;
126         }
127     }
128 
129     CPLIB_NORMALMESSAGE("Symbol: %s not found in CPLIB", symbol);
130 }
131 
132 template<typename _FuncType, typename... Args>
InvokeCpFunc(const char * symbol,Args...args)133 void CPLibUtils::InvokeCpFunc(const char* symbol, Args... args)
134 {
135     if (m_symbols.end() != m_symbols.find(symbol))
136     {
137         _FuncType func = reinterpret_cast<_FuncType>(m_symbols[symbol]);
138         if (nullptr != func)
139         {
140             func(args...);
141             return;
142         }
143     }
144 
145     CPLIB_NORMALMESSAGE("Symbol: %s not found in CPLIB", symbol);
146 }
147 
148 #endif
149