summaryrefslogtreecommitdiffstats
path: root/media/mca/filterfw/native/core/native_program.cpp
blob: c46c46fc401359e3b5609d55519b095d2407a97c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <dlfcn.h>

#include "base/logging.h"
#include "core/native_frame.h"
#include "core/native_program.h"

#include <string>
#include <vector>

namespace android {
namespace filterfw {

NativeProgram::NativeProgram()
    : lib_handle_(NULL),
      init_function_(NULL),
      setvalue_function_(NULL),
      getvalue_function_(NULL),
      process_function_(NULL),
      reset_function_(NULL),
      teardown_function_(NULL),
      user_data_(NULL) {
}

NativeProgram::~NativeProgram() {
  if (lib_handle_)
    dlclose(lib_handle_);
}

bool NativeProgram::OpenLibrary(const std::string& lib_name) {
  if (!lib_handle_) {
    lib_handle_ = dlopen(lib_name.c_str(), RTLD_NOW);
    if (!lib_handle_) {
      ALOGE("NativeProgram: Error opening library: '%s': %s", lib_name.c_str(), dlerror());
      return false;
    }
    return true;
  }
  return false;
}

bool NativeProgram::BindProcessFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  process_function_ = reinterpret_cast<ProcessFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  if (!process_function_) {
    ALOGE("NativeProgram: Could not find process function symbol: '%s'!", func_name.c_str());
    return false;
  }
  return true;
}

bool NativeProgram::BindInitFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  init_function_ = reinterpret_cast<InitFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  return init_function_ != NULL;
}

bool NativeProgram::BindSetValueFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  setvalue_function_ = reinterpret_cast<SetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  return setvalue_function_ != NULL;
}

bool NativeProgram::BindGetValueFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  getvalue_function_ = reinterpret_cast<GetValueFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  return getvalue_function_ != NULL;
}

bool NativeProgram::BindResetFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  reset_function_ = reinterpret_cast<ResetFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  return reset_function_ != NULL;
}

bool NativeProgram::BindTeardownFunction(const std::string& func_name) {
  if (!lib_handle_)
    return false;
  teardown_function_ = reinterpret_cast<TeardownFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
  return teardown_function_ != NULL;
}

bool NativeProgram::CallProcess(const std::vector<const char*>& inputs,
                                const std::vector<int>& input_sizes,
                                char* output,
                                int output_size) {
  if (process_function_) {
    return process_function_(const_cast<const char**>(&inputs[0]),
                             &input_sizes[0],
                             inputs.size(),
                             output,
                             output_size,
                             user_data_) == 1;
  }
  return false;
}

bool NativeProgram::CallInit() {
  if (init_function_) {
    init_function_(&user_data_);
    return true;
  }
  return false;
}

bool NativeProgram::CallSetValue(const std::string& key, const std::string& value) {
  if (setvalue_function_) {
    setvalue_function_(key.c_str(), value.c_str(), user_data_);
    return true;
  }
  return false;
}

std::string NativeProgram::CallGetValue(const std::string& key) {
  if (getvalue_function_) {
    static const int buffer_size = 1024;
    char result[buffer_size];
    result[buffer_size - 1] = '\0';
    getvalue_function_(key.c_str(), result, buffer_size, user_data_);
    return std::string(result);
  }
  return std::string();
}

bool NativeProgram::CallReset() {
  if (reset_function_) {
    reset_function_(user_data_);
    return true;
  }
  return false;
}

bool NativeProgram::CallTeardown() {
  if (teardown_function_) {
    teardown_function_(user_data_);
    return true;
  }
  return false;
}

} // namespace filterfw
} // namespace android