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 IMAGE_EFFECT_EFILTER_FACTORY_H 17 #define IMAGE_EFFECT_EFILTER_FACTORY_H 18 19 #include <functional> 20 #include <memory> 21 22 #include "delegate.h" 23 #include "efilter.h" 24 #include "image_effect_marco_define.h" 25 26 #define REGISTER_EFILTER_FACTORY(T, U) static AutoRegisterEFilter<T> gAutoRegster_##T(U) 27 28 namespace OHOS { 29 namespace Media { 30 namespace Effect { 31 using EFilterGenerator = std::function<std::shared_ptr<EFilter>(const std::string &name)>; 32 using EFilterInfoGetter = std::function<std::shared_ptr<EffectInfo>(const std::string &name)>; 33 34 struct EFilterFunction { 35 EFilterGenerator generator_; 36 EFilterInfoGetter infoGetter_; 37 }; 38 39 class EFilterFactory { 40 public: 41 ~EFilterFactory() = default; 42 43 EFilterFactory(const EFilterFactory &) = delete; 44 45 EFilterFactory operator = (const EFilterFactory &) = delete; 46 47 IMAGE_EFFECT_EXPORT static EFilterFactory *Instance(); 48 49 IMAGE_EFFECT_EXPORT void RegisterFunction(const std::string &name, const EFilterFunction &function); 50 51 IMAGE_EFFECT_EXPORT void RegisterDelegate(const std::string &name, const std::shared_ptr<IFilterDelegate> &delegate, 52 std::shared_ptr<EffectInfo> &effectInfo); 53 54 IMAGE_EFFECT_EXPORT std::shared_ptr<IFilterDelegate> GetDelegate(const std::string &name); 55 56 IMAGE_EFFECT_EXPORT std::shared_ptr<EFilter> Create(const std::string &name, void *handler); 57 Create(const std::string & name)58 inline std::shared_ptr<EFilter> Create(const std::string &name) 59 { 60 return Create(name, nullptr); 61 } 62 63 IMAGE_EFFECT_EXPORT 64 std::shared_ptr<EFilter> Restore(const std::string &name, const EffectJsonPtr &root, void *handler); 65 RegisterEFilter(const std::string & name)66 template <class T> void RegisterEFilter(const std::string &name) 67 { 68 EFilterFunction function = { 69 .generator_ = [](const std::string &name) { return std::make_shared<T>(name); }, 70 .infoGetter_ = [](const std::string &name) { return T::GetEffectInfo(name); } 71 }; 72 73 RegisterFunction(name, function); 74 } 75 76 IMAGE_EFFECT_EXPORT std::shared_ptr<EffectInfo> GetEffectInfo(const std::string &name); 77 78 IMAGE_EFFECT_EXPORT void GetAllEffectNames(std::vector<const char *> &names); 79 private: 80 EFilterFactory() = default; 81 82 std::map<std::string, EFilterFunction> functions_; 83 84 std::map<std::string, std::shared_ptr<IFilterDelegate>> delegates_; 85 }; 86 87 template <typename T> class AutoRegisterEFilter { 88 public: AutoRegisterEFilter(const std::string & name)89 explicit AutoRegisterEFilter(const std::string &name) 90 { 91 EFilterFactory::Instance()->RegisterEFilter<T>(name); 92 } 93 94 ~AutoRegisterEFilter() = default; 95 }; 96 } // namespace Effect 97 } // namespace Media 98 } // namespace OHOS 99 #endif // IMAGE_EFFECT_EFILTER_FACTORY_H 100