1 /* 2 * Copyright (c) 2024 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 #ifndef POST_PROCESSING_DYNAMIC_INTERFACE_H 17 #define POST_PROCESSING_DYNAMIC_INTERFACE_H 18 19 #include <array> 20 #include <type_traits> 21 #include "avcodec_errors.h" 22 #include "post_processing_utils.h" 23 #include "dynamic_interface_types.h" 24 25 namespace OHOS { 26 namespace MediaAVCodec { 27 namespace PostProcessing { 28 29 class DynamicInterface { 30 public: 31 ~DynamicInterface(); 32 33 bool Load(); 34 void Unload(); 35 36 template<DynamicInterfaceName E, typename... Args, typename RetT = DynamicInterfaceReturnType<E, Args...>> Invoke(Args &&...args)37 RetT Invoke(Args&& ... args) 38 { 39 constexpr DynamicInterfaceIndexType<E> I = DynamicInterfaceIndexValue<E>; 40 AVCODEC_LOGD("Invoke %{public}s", DYNAMIC_INTERFACE_SYMBOLS[I]); 41 auto interface = reinterpret_cast<DynamicInterfaceFuncTypes::Get<I>>(interfaces_[I]); 42 if constexpr (std::is_void_v<RetT>) { 43 CHECK_AND_RETURN_LOG(interface != nullptr, "Interface not found."); 44 interface(std::forward<Args>(args)...); 45 } else if constexpr (std::is_pointer_v<RetT>) { 46 CHECK_AND_RETURN_RET_LOG(interface != nullptr, nullptr, "Interface not found."); 47 return interface(std::forward<Args>(args)...); 48 } else if constexpr (std::is_integral_v<RetT>) { 49 CHECK_AND_RETURN_RET_LOG(interface != nullptr, AVCS_ERR_INVALID_VAL, "Interface not found."); 50 return interface(std::forward<Args>(args)...); 51 } 52 } 53 private: 54 bool OpenLibrary(); 55 void CloseLibrary(); 56 bool ReadSymbols(); 57 void ClearSymbols(); 58 59 void* lib_{nullptr}; 60 std::array<void*, DYNAMIC_INTERFACE_NUM> interfaces_{nullptr}; 61 62 static constexpr HiviewDFX::HiLogLabel LABEL{LogLabel("DynamicInterface")}; 63 }; 64 65 } // namespace PostProcessing 66 } // namespace MediaAVCodec 67 } // OHOS 68 69 #endif // POST_PROCESSING_DYNAMIC_INTERFACE_H