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 #include "efilter_factory.h"
17
18 #include "custom_efilter.h"
19 #include "effect_log.h"
20 #include "external_loader.h"
21 #include "json_helper.h"
22
23 namespace OHOS {
24 namespace Media {
25 namespace Effect {
Instance()26 EFilterFactory *EFilterFactory::Instance()
27 {
28 static EFilterFactory instance;
29 return &instance;
30 }
31
RegisterFunction(const std::string & name,const EFilterFunction & function)32 void EFilterFactory::RegisterFunction(const std::string &name, const EFilterFunction &function)
33 {
34 EFFECT_LOGI("register efilter. name=%{public}s", name.c_str());
35
36 auto it = functions_.find(name);
37 if (it == functions_.end()) {
38 auto result = functions_.emplace(name, function);
39 if (!result.second) {
40 result.first->second = function;
41 }
42 } else {
43 functions_[name] = function;
44 }
45 }
46
RegisterDelegate(const std::string & name,const std::shared_ptr<IFilterDelegate> & delegate,std::shared_ptr<EffectInfo> & effectInfo)47 void EFilterFactory::RegisterDelegate(const std::string &name, const std::shared_ptr<IFilterDelegate> &delegate,
48 std::shared_ptr<EffectInfo> &effectInfo)
49 {
50 EFFECT_LOGI("register delegate. name=%{public}s", name.c_str());
51 ExternLoader::Instance()->InitExt();
52 RegisterEFilter<CustomEFilter>(name);
53
54 CustomEFilter::SetEffectInfo(name, effectInfo);
55
56 auto it = delegates_.find(name);
57 if (it == delegates_.end()) {
58 auto result = delegates_.emplace(name, delegate);
59 if (!result.second) {
60 result.first->second = delegate;
61 }
62 } else {
63 delegates_[name] = delegate;
64 }
65 }
66
GetDelegate(const std::string & name)67 std::shared_ptr<IFilterDelegate> EFilterFactory::GetDelegate(const std::string &name)
68 {
69 auto it = delegates_.find(name);
70 if (it != delegates_.end()) {
71 return it->second;
72 }
73 return nullptr;
74 }
75
Restore(const std::string & name,const EffectJsonPtr & root,void * handler)76 std::shared_ptr<EFilter> EFilterFactory::Restore(const std::string &name, const EffectJsonPtr &root, void *handler)
77 {
78 std::shared_ptr<EFilter> efilter = EFilterFactory::Instance()->Create(name, handler);
79 CHECK_AND_RETURN_RET_LOG(efilter != nullptr, nullptr, "Restore: create filter fail! name=%{public}s", name.c_str());
80 CHECK_AND_RETURN_RET_LOG(root->HasElement("values"), efilter, "[values] not exist!");
81
82 const EffectJsonPtr values = root->GetElement("values");
83 CHECK_AND_RETURN_RET_LOG(efilter->Restore(values) == ErrorCode::SUCCESS, efilter, "values restore fail!");
84 return efilter;
85 }
86
Create(const std::string & name,void * handler)87 std::shared_ptr<EFilter> EFilterFactory::Create(const std::string &name, void *handler)
88 {
89 ExternLoader::Instance()->InitExt();
90 auto it = functions_.find(name);
91 if (it != functions_.end()) {
92 std::shared_ptr<EFilter> efilter = it->second.generator_(name);
93 if (GetDelegate(name)) {
94 static_cast<CustomEFilter *>(efilter.get())->SetHandler(handler);
95 }
96 return efilter;
97 }
98 EFFECT_LOGE("create effect fail! functions has no %{public}s", name.c_str());
99 return nullptr;
100 }
101
GetEffectInfo(const std::string & name)102 std::shared_ptr<EffectInfo> EFilterFactory::GetEffectInfo(const std::string &name)
103 {
104 ExternLoader::Instance()->InitExt();
105 auto it = functions_.find(name);
106 if (it != functions_.end()) {
107 return it->second.infoGetter_(name);
108 }
109 EFFECT_LOGE("get effect info fail! functions has no %{public}s", name.c_str());
110 return nullptr;
111 }
112
GetAllEffectNames(std::vector<const char * > & names)113 void EFilterFactory::GetAllEffectNames(std::vector<const char *> &names)
114 {
115 std::transform(functions_.begin(), functions_.end(), std::back_inserter(names),
116 [](const auto &pair) { return pair.first.c_str(); });
117 }
118 } // namespace Effect
119 } // namespace Media
120 } // namespace OHOS