Add stereoConfigPath to get stereoConfig information for point cloud generation
[platform/core/multimedia/dfs-adaptation.git] / src / dfs_adaptation_impl.cpp
1 /**
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dlfcn.h>
18 #include <iniparser.h>
19 #include <string.h>
20 #include "dfs_adaptation_impl.h"
21
22 extern "C"
23 {
24 #include <dlog.h>
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "DFS_ADAPTATION"
31 }
32
33 #define CHECK_INSTANCE(object)       \
34         if (object == nullptr) {         \
35                 throw std::invalid_argument("handle is null"); \
36         }
37
38 namespace DfsAdaptation
39 {
40         const char *BACKEND_PATH_INI_FILENAME =
41                                 SYSCONFDIR"/vision/depth/backend_path.ini";
42
43         DfsAdaptor::DfsAdaptor() :
44                 mDfsAdaptor(nullptr),
45                 mDfsAdaptorHandle(nullptr)
46         {
47                 LOGI("ENTER");
48                 LOGI("LEAVE");
49         }
50
51         DfsAdaptor::~DfsAdaptor()
52         {
53                 LOGI("ENTER");
54                 LOGI("LEAVE");
55         }
56
57
58         void DfsAdaptor::Bind()
59         {
60                 LOGI("ENTER");
61
62                 dictionary *dict = iniparser_load(BACKEND_PATH_INI_FILENAME);
63                 if (!dict) {
64                         throw std::invalid_argument("invalid backend path ini");
65                 }
66
67                 std::string adaptorName = std::string(iniparser_getstring(dict, "DFS backend:type", NULL));
68                 iniparser_freedict(dict);
69
70                 if (adaptorName.empty()) {
71                         throw std::runtime_error("not supported dfs backend");
72                 }
73
74                 std::string adaptorLibName = "libdfs-" + adaptorName + ".so";
75
76                 mDfsAdaptor = dlopen(adaptorLibName.c_str(), RTLD_NOW);
77                 LOGI("%s DfsAdaptor: %p", adaptorLibName.c_str(), mDfsAdaptor);
78
79                 if (!mDfsAdaptor) {
80                         LOGE("Failed to open %s: %s", adaptorLibName.c_str(), dlerror());
81                         throw std::runtime_error(adaptorLibName.c_str());
82                 }
83
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");
89                         dlclose(mDfsAdaptor);
90                         mDfsAdaptor = nullptr;
91                         throw std::runtime_error(adaptorLibName.c_str());
92                 }
93
94                 mDfsAdaptorHandle = AdaptorInit();
95                 if (!mDfsAdaptorHandle) {
96                         LOGE("Failed to AdaptorInit");
97                         dlclose(mDfsAdaptor);
98                         mDfsAdaptor = nullptr;
99                         throw std::runtime_error(adaptorLibName.c_str());
100                 }
101
102                 LOGI("LEAVE");
103         }
104
105         void DfsAdaptor::Unbind()
106         {
107                 LOGI("ENTER");
108
109                 if (mDfsAdaptor) {
110                         destroy_t *adaptorDestroy = (destroy_t*)dlsym(mDfsAdaptor, "AdaptorDestroy");
111                         adaptorDestroy(mDfsAdaptorHandle);
112                         dlclose(mDfsAdaptor);
113                         mDfsAdaptor = mDfsAdaptorHandle = nullptr;
114                 }
115
116                 LOGI("LEAVE");
117         }
118
119         void DfsAdaptor::Initialize(DfsParameter& param, size_t width, size_t height,
120                                                                 size_t minDisp, size_t maxDisp, std::string stereoConfigPath)
121         {
122                 LOGI("ENTER");
123
124                 CHECK_INSTANCE(mDfsAdaptorHandle);
125
126                 mDfsAdaptorHandle->Initialize(param, width, height, minDisp, maxDisp, stereoConfigPath);
127
128                 LOGI("LEAVE");
129         }
130
131         void DfsAdaptor::Run(DfsData& base, DfsData& extra)
132         {
133                 LOGI("ENTER");
134
135                 CHECK_INSTANCE(mDfsAdaptorHandle);
136
137                 mDfsAdaptorHandle->Run(base, extra);
138
139                 LOGI("LEAVE");
140         }
141
142         DfsData& DfsAdaptor::GetDepthData()
143         {
144                 LOGI("ENTER");
145
146                 CHECK_INSTANCE(mDfsAdaptorHandle);
147
148                 return mDfsAdaptorHandle->GetDepthData();
149
150                 LOGI("LEAVE");
151         }
152 }