2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <iniparser.h>
20 #include "dfs_adaptation_impl.h"
30 #define LOG_TAG "DFS_ADAPTATION"
33 #define CHECK_INSTANCE(object) \
34 if (object == nullptr) { \
35 throw std::invalid_argument("handle is null"); \
38 namespace DfsAdaptation
40 const char *BACKEND_PATH_INI_FILENAME =
41 SYSCONFDIR"/vision/depth/backend_path.ini";
43 DfsAdaptor::DfsAdaptor() :
45 mDfsAdaptorHandle(nullptr)
51 DfsAdaptor::~DfsAdaptor()
58 void DfsAdaptor::Bind()
62 dictionary *dict = iniparser_load(BACKEND_PATH_INI_FILENAME);
64 throw std::invalid_argument("invalid backend path ini");
67 std::string adaptorName = std::string(iniparser_getstring(dict, "DFS backend:type", NULL));
68 iniparser_freedict(dict);
70 if (adaptorName.empty()) {
71 throw std::runtime_error("not supported dfs backend");
74 std::string adaptorLibName = "libdfs-" + adaptorName + ".so";
76 mDfsAdaptor = dlopen(adaptorLibName.c_str(), RTLD_NOW);
77 LOGI("%s DfsAdaptor: %p", adaptorLibName.c_str(), mDfsAdaptor);
80 LOGE("Failed to open %s: %s", adaptorLibName.c_str(), dlerror());
81 throw std::runtime_error(adaptorLibName.c_str());
84 dlerror(); /* clear any old error conditions */
85 init_t *AdaptorInit = (init_t*)dlsym(mDfsAdaptor, "AdaptorInit");
86 char *error = dlerror();
87 if (error || !AdaptorInit) {
88 LOGE("dlsym returns %p and error %s", AdaptorInit, error ? error : "none");
90 mDfsAdaptor = nullptr;
91 throw std::runtime_error(adaptorLibName.c_str());
94 mDfsAdaptorHandle = AdaptorInit();
95 if (!mDfsAdaptorHandle) {
96 LOGE("Failed to AdaptorInit");
98 mDfsAdaptor = nullptr;
99 throw std::runtime_error(adaptorLibName.c_str());
105 void DfsAdaptor::Unbind()
110 destroy_t *adaptorDestroy = (destroy_t*)dlsym(mDfsAdaptor, "AdaptorDestroy");
111 adaptorDestroy(mDfsAdaptorHandle);
112 dlclose(mDfsAdaptor);
113 mDfsAdaptor = mDfsAdaptorHandle = nullptr;
119 void DfsAdaptor::Initialize(DfsParameter& param, size_t width, size_t height,
120 size_t minDisp, size_t maxDisp, std::string stereoConfigPath)
124 CHECK_INSTANCE(mDfsAdaptorHandle);
126 mDfsAdaptorHandle->Initialize(param, width, height, minDisp, maxDisp, stereoConfigPath);
131 void DfsAdaptor::Run(DfsData& base, DfsData& extra)
135 CHECK_INSTANCE(mDfsAdaptorHandle);
137 mDfsAdaptorHandle->Run(base, extra);
142 DfsData& DfsAdaptor::GetDepthData()
146 CHECK_INSTANCE(mDfsAdaptorHandle);
148 return mDfsAdaptorHandle->GetDepthData();