2 * Copyright (C) 2011 The Android Open Source Project
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.
19 #include "base/logging.h"
20 #include "core/native_frame.h"
21 #include "core/native_program.h"
29 NativeProgram::NativeProgram()
32 setvalue_function_(NULL),
33 getvalue_function_(NULL),
34 process_function_(NULL),
35 reset_function_(NULL),
36 teardown_function_(NULL),
40 NativeProgram::~NativeProgram() {
45 bool NativeProgram::OpenLibrary(const std::string& lib_name) {
47 lib_handle_ = dlopen(lib_name.c_str(), RTLD_NOW);
49 LOGE("NativeProgram: Could not find library: '%s'!", lib_name.c_str());
57 bool NativeProgram::BindProcessFunction(const std::string& func_name) {
60 process_function_ = reinterpret_cast<ProcessFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
61 if (!process_function_) {
62 LOGE("NativeProgram: Could not find process function symbol: '%s'!", func_name.c_str());
68 bool NativeProgram::BindInitFunction(const std::string& func_name) {
71 init_function_ = reinterpret_cast<InitFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
72 return init_function_ != NULL;
75 bool NativeProgram::BindSetValueFunction(const std::string& func_name) {
78 setvalue_function_ = reinterpret_cast<SetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
79 return setvalue_function_ != NULL;
82 bool NativeProgram::BindGetValueFunction(const std::string& func_name) {
85 getvalue_function_ = reinterpret_cast<GetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
86 return getvalue_function_ != NULL;
89 bool NativeProgram::BindResetFunction(const std::string& func_name) {
92 reset_function_ = reinterpret_cast<ResetFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
93 return reset_function_ != NULL;
96 bool NativeProgram::BindTeardownFunction(const std::string& func_name) {
99 teardown_function_ = reinterpret_cast<TeardownFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
100 return teardown_function_ != NULL;
103 bool NativeProgram::CallProcess(const std::vector<const char*>& inputs,
104 const std::vector<int>& input_sizes,
107 if (process_function_) {
108 return process_function_(const_cast<const char**>(&inputs[0]),
118 bool NativeProgram::CallInit() {
119 if (init_function_) {
120 init_function_(&user_data_);
126 bool NativeProgram::CallSetValue(const std::string& key, const std::string& value) {
127 if (setvalue_function_) {
128 setvalue_function_(key.c_str(), value.c_str(), user_data_);
134 std::string NativeProgram::CallGetValue(const std::string& key) {
135 if (getvalue_function_) {
136 static const int buffer_size = 1024;
137 char result[buffer_size];
138 result[buffer_size - 1] = '\0';
139 getvalue_function_(key.c_str(), result, buffer_size, user_data_);
140 return std::string(result);
142 return std::string();
145 bool NativeProgram::CallReset() {
146 if (reset_function_) {
147 reset_function_(user_data_);
153 bool NativeProgram::CallTeardown() {
154 if (teardown_function_) {
155 teardown_function_(user_data_);
161 } // namespace filterfw
162 } // namespace android