1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

android -> plarforms/android

This commit is contained in:
Alexander Smorkalov
2013-05-17 17:15:20 +04:00
parent 324cafdda6
commit 20fef00a77
134 changed files with 34 additions and 27 deletions
@@ -0,0 +1,72 @@
#include "EngineCommon.h"
#include "IOpenCVEngine.h"
#include "BnOpenCVEngine.h"
#include <utils/Log.h>
#include <utils/String8.h>
#include <utils/String16.h>
using namespace android;
BnOpenCVEngine::~BnOpenCVEngine()
{
}
// Notes about data transaction:
// Java Binder Wrapper call readInt32 before reading return data
// It treet this in value as exception code
// OnTransact method support this feature
status_t BnOpenCVEngine::onTransact(uint32_t code, const Parcel& data, android::Parcel* reply, uint32_t flags)
{
LOGD("OpenCVEngine::OnTransact(%u,%u)", code, flags);
switch(code)
{
case OCVE_GET_ENGINE_VERSION:
{
LOGD("OpenCVEngine OCVE_GET_ENGINE_VERSION request");
CHECK_INTERFACE(IOpenCVEngine, data, reply);
LOGD("OpenCVEngine::GetVersion()");
reply->writeInt32(0);
return reply->writeInt32(GetVersion());
} break;
case OCVE_GET_LIB_PATH_BY_VERSION:
{
LOGD("OpenCVEngine OCVE_GET_LIB_PATH_BY_VERSION request");
CHECK_INTERFACE(IOpenCVEngine, data, reply);
const String16 version = data.readString16();
LOGD("OpenCVEngine::GetLibPathByVersion(%s)", String8(version).string());
String16 path = GetLibPathByVersion(version);
reply->writeInt32(0);
return reply->writeString16(path);
} break;
case OCVE_GET_LIB_LIST:
{
LOGD("OpenCVEngine OCVE_GET_LIB_LIST request");
CHECK_INTERFACE(IOpenCVEngine, data, reply);
const String16 version = data.readString16();
LOGD("OpenCVEngine::GetLibraryList(%s)", String8(version).string());
String16 path = GetLibraryList(version);
reply->writeInt32(0);
return reply->writeString16(path);
} break;
case OCVE_INSTALL_VERSION:
{
LOGD("OpenCVEngine OCVE_INSTALL_VERSION request");
CHECK_INTERFACE(IOpenCVEngine, data, reply);
const String16 version = data.readString16();
LOGD("OpenCVEngine::InstallVersion(%s)", String8(version).string());
bool result = InstallVersion(version);
reply->writeInt32(0);
int res = reply->writeInt32(static_cast<int32_t>(result));
LOGD("InstallVersion call to Binder finished with res %d", res);
return res;
} break;
default:
{
LOGD("OpenCVEngine unknown request");
return BBinder::onTransact(code, data, reply, flags);
}
}
return android::NO_ERROR;
}
@@ -0,0 +1,21 @@
#ifndef __BP_OPENCV_ENGINE_H__
#define __BP_OPENCV_ENGINE_H__
#include "EngineCommon.h"
#include "IOpenCVEngine.h"
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/String16.h>
class BnOpenCVEngine: public android::BnInterface<IOpenCVEngine>
{
public:
android::status_t onTransact(uint32_t code,
const android::Parcel &data,
android::Parcel *reply,
uint32_t flags);
virtual ~BnOpenCVEngine();
};
#endif
@@ -0,0 +1,71 @@
#include "IOpenCVEngine.h"
#include "BpOpenCVEngine.h"
using namespace android;
BpOpenCVEngine::BpOpenCVEngine(const sp<IBinder>& impl):
BpInterface<IOpenCVEngine>(impl)
{
}
BpOpenCVEngine::~BpOpenCVEngine()
{
}
// Notes about data transaction:
// Java Binder Wrapper call readInt32 before reading return data
// It treet this in value as exception code
// This implementation support this feature
int BpOpenCVEngine::GetVersion()
{
Parcel data, reply;
data.writeInterfaceToken(IOpenCVEngine::descriptor);
remote()->transact(OCVE_GET_ENGINE_VERSION, data, &reply, 0);
// read exception code
reply.readInt32();
return reply.readInt32();
}
String16 BpOpenCVEngine::GetLibPathByVersion(String16 version)
{
Parcel data, reply;
data.writeInterfaceToken(IOpenCVEngine::descriptor);
data.writeString16(version);
remote()->transact(OCVE_GET_LIB_PATH_BY_VERSION, data, &reply, 0);
// read exception code
reply.readInt32();
return reply.readString16();
}
android::String16 BpOpenCVEngine::GetLibraryList(String16 version)
{
Parcel data, reply;
data.writeInterfaceToken(IOpenCVEngine::descriptor);
data.writeString16(version);
remote()->transact(OCVE_GET_LIB_LIST, data, &reply, 0);
// read exception code
reply.readInt32();
return reply.readString16();
}
bool BpOpenCVEngine::InstallVersion(String16 version)
{
Parcel data, reply;
data.writeInterfaceToken(IOpenCVEngine::descriptor);
data.writeString16(version);
remote()->transact(OCVE_INSTALL_VERSION, data, &reply, 0);
// read exception code
reply.readInt32();
return static_cast<bool>(reply.readInt32());
}
IMPLEMENT_META_INTERFACE(OpenCVEngine, OPECV_ENGINE_CLASSNAME)
@@ -0,0 +1,20 @@
#ifndef __BP_OPENCV_ENGINE_H__
#define __BP_OPENCV_ENGINE_H__
#include "IOpenCVEngine.h"
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/String16.h>
class BpOpenCVEngine: public android::BpInterface<IOpenCVEngine>
{
public:
BpOpenCVEngine(const android::sp<android::IBinder>& impl);
virtual ~BpOpenCVEngine();
virtual int GetVersion();
virtual android::String16 GetLibPathByVersion(android::String16 version);
virtual android::String16 GetLibraryList(android::String16 version);
virtual bool InstallVersion(android::String16 version);
};
#endif
@@ -0,0 +1,175 @@
#include "HardwareDetector.h"
#include "TegraDetector.h"
#include "ProcReader.h"
#include "EngineCommon.h"
#include "StringUtils.h"
#include <utils/Log.h>
using namespace std;
int GetCpuID()
{
int result = 0;
map<string, string> cpu_info = GetCpuInfo();
map<string, string>::const_iterator it;
#if defined(__i386__)
LOGD("Using X86 HW detector");
result |= ARCH_X86;
it = cpu_info.find("flags");
if (cpu_info.end() != it)
{
set<string> features = SplitString(it->second, ' ');
if (features.end() != features.find(CPU_INFO_SSE_STR))
{
result |= FEATURES_HAS_SSE;
}
if (features.end() != features.find(CPU_INFO_SSE2_STR))
{
result |= FEATURES_HAS_SSE2;
}
if (features.end() != features.find(CPU_INFO_SSSE3_STR))
{
result |= FEATURES_HAS_SSSE3;
}
}
#elif defined(__mips)
#ifdef __SUPPORT_MIPS
result |= ARCH_MIPS;
#else
result = ARCH_UNKNOWN;
#endif
#else
LOGD("Using ARM HW detector");
it = cpu_info.find("Processor");
if (cpu_info.end() != it)
{
size_t proc_name_pos = it->second.find(CPU_INFO_ARCH_X86_STR);
if (string::npos != proc_name_pos)
{
}
else
{
proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV7_STR);
if (string::npos != proc_name_pos)
{
result |= ARCH_ARMv7;
}
else
{
proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV6_STR);
if (string::npos != proc_name_pos)
{
result |= ARCH_ARMv6;
}
else
{
proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV5_STR);
if (string::npos != proc_name_pos)
{
result |= ARCH_ARMv5;
}
}
}
}
}
else
{
return ARCH_UNKNOWN;
}
it = cpu_info.find("Features");
if (cpu_info.end() != it)
{
set<string> features = SplitString(it->second, ' ');
if (features.end() != features.find(CPU_INFO_NEON_STR))
{
result |= FEATURES_HAS_NEON;
}
if (features.end() != features.find(CPU_INFO_NEON2_STR))
{
result |= FEATURES_HAS_NEON2;
}
if (features.end() != features.find(CPU_INFO_VFPV3_STR))
{
if (features.end () != features.find(CPU_INFO_VFPV3D16_STR))
{
result |= FEATURES_HAS_VFPv3d16;
}
else
{
result |= FEATURES_HAS_VFPv3;
}
}
}
#endif
return result;
}
string GetPlatformName()
{
map<string, string> cpu_info = GetCpuInfo();
string hardware_name = "";
map<string, string>::const_iterator hw_iterator = cpu_info.find("Hardware");
if (cpu_info.end() != hw_iterator)
{
hardware_name = hw_iterator->second;
}
return hardware_name;
}
int GetProcessorCount()
{
FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r");
if(!cpuPossible)
return 1;
char buf[2000]; //big enough for 1000 CPUs in worst possible configuration
char* pbuf = fgets(buf, sizeof(buf), cpuPossible);
fclose(cpuPossible);
if(!pbuf)
return 1;
//parse string of form "0-1,3,5-7,10,13-15"
int cpusAvailable = 0;
while(*pbuf)
{
const char* pos = pbuf;
bool range = false;
while(*pbuf && *pbuf != ',')
{
if(*pbuf == '-') range = true;
++pbuf;
}
if(*pbuf) *pbuf++ = 0;
if(!range)
++cpusAvailable;
else
{
int rstart = 0, rend = 0;
sscanf(pos, "%d-%d", &rstart, &rend);
cpusAvailable += rend - rstart + 1;
}
}
return cpusAvailable ? cpusAvailable : 1;
}
int DetectKnownPlatforms()
{
int tegra_status = DetectTegra();
// All Tegra platforms since Tegra3
if (2 < tegra_status)
{
return PLATFORM_TEGRA + tegra_status - 1;
}
else
{
return PLATFORM_UNKNOWN;
}
}
@@ -0,0 +1,37 @@
#ifndef __HARDWARE_DETECTOR_H__
#define __HARDWARE_DETECTOR_H__
#include <string>
#define ARCH_UNKNOWN 0L
#define ARCH_X86 16777216L
#define ARCH_X64 33554432L
#define ARCH_ARMv5 67108864L
#define ARCH_ARMv6 134217728L
#define ARCH_ARMv7 268435456L
#define ARCH_ARMv8 536870912L
#define ARCH_MIPS 1073741824L
#define FEATURES_HAS_VFPv3d16 1L
#define FEATURES_HAS_VFPv3 2L
#define FEATURES_HAS_NEON 4L
#define FEATURES_HAS_NEON2 8L
#define FEATURES_HAS_SSE 1L
#define FEATURES_HAS_SSE2 2L
#define FEATURES_HAS_SSSE3 4L
#define FEATURES_HAS_GPU 65536L
// TODO: Do not forget to add Platrfom name to PackageInfo::PlatformNameMap
// in method PackageInfo::InitPlatformNameMap()
#define PLATFORM_UNKNOWN 0L
#define PLATFORM_TEGRA 1L
#define PLATFORM_TEGRA2 2L
#define PLATFORM_TEGRA3 3L
#define PLATFORM_TEGRA4 4L
int DetectKnownPlatforms();
int GetProcessorCount();
std::string GetPlatformName();
int GetCpuID();
#endif
@@ -0,0 +1,202 @@
#include "EngineCommon.h"
#include "OpenCVEngine.h"
#include "HardwareDetector.h"
#include "StringUtils.h"
#include <utils/Log.h>
#include <assert.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <dlfcn.h>
using namespace android;
const int OpenCVEngine::Platform = DetectKnownPlatforms();
const int OpenCVEngine::CpuID = GetCpuID();
const int OpenCVEngine::KnownVersions[] = {2040000, 2040100, 2040200, 2040300, 2040301, 2040302, 2040400, 2040500};
bool OpenCVEngine::ValidateVersion(int version)
{
for (size_t i = 0; i < sizeof(KnownVersions)/sizeof(int); i++)
if (KnownVersions[i] == version)
return true;
return false;
}
int OpenCVEngine::NormalizeVersionString(std::string version)
{
int result = 0;
if (version.empty())
{
return result;
}
std::vector<std::string> parts = SplitStringVector(version, '.');
// Use only 4 digits of the version, i.e. 1.2.3.4.
// Other digits will be ignored.
if (parts.size() > 4)
parts.erase(parts.begin()+4, parts.end());
int multiplyer = 1000000;
for (std::vector<std::string>::const_iterator it = parts.begin(); it != parts.end(); ++it)
{
int digit = atoi(it->c_str());
result += multiplyer*digit;
multiplyer /= 100;
}
if (!ValidateVersion(result))
result = 0;
return result;
}
OpenCVEngine::OpenCVEngine(IPackageManager* PkgManager):
PackageManager(PkgManager)
{
assert(PkgManager);
}
int32_t OpenCVEngine::GetVersion()
{
return OPEN_CV_ENGINE_VERSION;
}
String16 OpenCVEngine::GetLibPathByVersion(android::String16 version)
{
std::string std_version(String8(version).string());
int norm_version;
std::string path;
LOGD("OpenCVEngine::GetLibPathByVersion(%s) impl", String8(version).string());
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
{
path = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
if (path.empty())
{
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", String8(version).string(), norm_version);
}
else
{
FixPermissions(path);
}
}
else
{
LOGE("OpenCV version \"%s\" (%d) is not supported", String8(version).string(), norm_version);
}
return String16(path.c_str());
}
android::String16 OpenCVEngine::GetLibraryList(android::String16 version)
{
std::string std_version = String8(version).string();
int norm_version;
String16 result;
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
{
std::string tmp = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
if (!tmp.empty())
{
tmp += (std::string("/") + LIB_OPENCV_INFO_NAME);
LOGD("Trying to load info library \"%s\"", tmp.c_str());
void* handle;
InfoFunctionType info_func;
handle = dlopen(tmp.c_str(), RTLD_LAZY);
if (handle)
{
const char* error;
dlerror();
info_func = (InfoFunctionType)dlsym(handle, "GetLibraryList");
if ((error = dlerror()) == NULL)
{
result = String16((*info_func)());
dlclose(handle);
}
else
{
LOGE("Library loading error: \"%s\"", error);
}
}
else
{
LOGI("Info library not found in package");
}
}
else
{
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", std_version.c_str(), norm_version);
}
}
else
{
LOGE("OpenCV version \"%s\" is not supported", std_version.c_str());
}
return result;
}
bool OpenCVEngine::InstallVersion(android::String16 version)
{
std::string std_version = String8(version).string();
int norm_version;
bool result = false;
LOGD("OpenCVEngine::InstallVersion() begin");
norm_version = NormalizeVersionString(std_version);
if (0 != norm_version)
{
LOGD("PackageManager->InstallVersion call");
result = PackageManager->InstallVersion(norm_version, Platform, CpuID);
}
else
{
LOGE("OpenCV version \"%s\" (%d) is not supported", std_version.c_str(), norm_version);
}
LOGD("OpenCVEngine::InstallVersion() end");
return result;
}
bool OpenCVEngine::FixPermissions(const std::string& path)
{
LOGD("Fixing permissions for folder: \"%s\"", path.c_str());
chmod(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
DIR* dir = opendir(path.c_str());
if (!dir)
{
LOGD("Fixing permissions error");
return false;
}
dirent* files = readdir(dir);
while (files)
{
LOGD("Fix permissions for \"%s\"", files->d_name);
chmod((path + std::string("/") + std::string(files->d_name)).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
files = readdir(dir);
}
closedir(dir);
return true;
}
@@ -0,0 +1,37 @@
#ifndef __OPEN_CV_ENGINE_H__
#define __OPEN_CV_ENGINE_H__
#include "EngineCommon.h"
#include "IOpenCVEngine.h"
#include "BnOpenCVEngine.h"
#include "IPackageManager.h"
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/String8.h>
#include <utils/String16.h>
#include <string>
#include <set>
class OpenCVEngine: public BnOpenCVEngine
{
public:
OpenCVEngine(IPackageManager* PkgManager);
int32_t GetVersion();
android::String16 GetLibPathByVersion(android::String16 version);
virtual android::String16 GetLibraryList(android::String16 version);
bool InstallVersion(android::String16 version);
protected:
IPackageManager* PackageManager;
static const int KnownVersions[];
OpenCVEngine();
bool ValidateVersion(int version);
int NormalizeVersionString(std::string version);
bool FixPermissions(const std::string& path);
static const int Platform;
static const int CpuID;
};
#endif
@@ -0,0 +1,31 @@
#include "ProcReader.h"
#include "StringUtils.h"
#include <fstream>
using namespace std;
map<string, string> GetCpuInfo()
{
map<string, string> result;
ifstream f;
f.open("/proc/cpuinfo");
if (f.is_open())
{
while (!f.eof())
{
string tmp;
string key;
string value;
getline(f, tmp);
if (ParseString(tmp, key, value))
{
result[key] = value;
}
}
}
f.close();
return result;
}
@@ -0,0 +1,29 @@
#ifndef __PROC_READER_H__
#define __PROC_READER_H__
#include <map>
#include <set>
#include <string>
#define CPU_INFO_NEON_STR "neon"
#define CPU_INFO_NEON2_STR "neon2"
#define CPU_INFO_VFPV3_STR "vfpv3"
#define CPU_INFO_VFPV3D16_STR "vfpv3d16"
#define CPU_INFO_SSE_STR "sse"
#define CPU_INFO_SSE2_STR "sse2"
#define CPU_INFO_SSSE3_STR "ssse3"
#define CPU_INFO_ARCH_ARMV7_STR "(v7l)"
#define CPU_INFO_ARCH_ARMV6_STR "(v6l)"
#define CPU_INFO_ARCH_ARMV5_STR "(v5l)"
#define CPU_INFO_ARCH_X86_STR "x86"
#define CPU_INFO_ARCH_MIPS_STR "MIPS"
// public part
std::map<std::string, std::string> GetCpuInfo();
#endif
@@ -0,0 +1,92 @@
#include "StringUtils.h"
using namespace std;
bool StripString(string& src)
{
size_t pos = 0;
if (src.empty())
{
return false;
}
while ((pos < src.length()) && (' ' == src[pos])) pos++;
src.erase(0, pos);
pos = 0;
while ((pos < src.length()) && ('\t' == src[pos])) pos++;
src.erase(0, pos);
pos = src.length() - 1;
while (pos && (' ' == src[pos])) pos--;
src.erase(pos+1);
pos = src.length() - 1;
while (pos && ('\t' == src[pos])) pos--;
src.erase(pos+1);
return true;
}
bool ParseString(const string& src, string& key, string& value)
{
if (src.empty())
return false;
// find seporator ":"
size_t seporator_pos = src.find(":");
if (string::npos != seporator_pos)
{
key = src.substr(0, seporator_pos);
StripString(key);
value = src.substr(seporator_pos+1);
StripString(value);
return true;
}
else
{
return false;
}
}
set<string> SplitString(const string& src, const char seporator)
{
set<string> result;
if (!src.empty())
{
size_t seporator_pos;
size_t prev_pos = 0;
do
{
seporator_pos = src.find(seporator, prev_pos);
result.insert(src.substr(prev_pos, seporator_pos - prev_pos));
prev_pos = seporator_pos + 1;
}
while (string::npos != seporator_pos);
}
return result;
}
vector<string> SplitStringVector(const string& src, const char seporator)
{
vector<string> result;
if (!src.empty())
{
size_t seporator_pos;
size_t prev_pos = 0;
do
{
seporator_pos = src.find(seporator, prev_pos);
string tmp = src.substr(prev_pos, seporator_pos - prev_pos);
result.push_back(tmp);
prev_pos = seporator_pos + 1;
}
while (string::npos != seporator_pos);
}
return result;
}
@@ -0,0 +1,13 @@
#ifndef __STRING_UTILS_H__
#define __STRING_UTILS_H__
#include <string>
#include <set>
#include <vector>
bool StripString(std::string& src);
std::set<std::string> SplitString(const std::string& src, const char seporator);
bool ParseString(const std::string& src, std::string& key, std::string& value);
std::vector<std::string> SplitStringVector(const std::string& src, const char seporator);
#endif
@@ -0,0 +1,61 @@
#include "TegraDetector.h"
#include <zlib.h>
#include <string.h>
#define KERNEL_CONFIG "/proc/config.gz"
#define KERNEL_CONFIG_MAX_LINE_WIDTH 512
#define KERNEL_CONFIG_TEGRA_MAGIC "CONFIG_ARCH_TEGRA=y"
#define KERNEL_CONFIG_TEGRA2_MAGIC "CONFIG_ARCH_TEGRA_2x_SOC=y"
#define KERNEL_CONFIG_TEGRA3_MAGIC "CONFIG_ARCH_TEGRA_3x_SOC=y"
#define KERNEL_CONFIG_TEGRA4_MAGIC "CONFIG_ARCH_TEGRA_11x_SOC=y"
#define MAX_DATA_LEN 4096
int DetectTegra()
{
int result = TEGRA_NOT_TEGRA;
gzFile kernelConfig = gzopen(KERNEL_CONFIG, "r");
if (kernelConfig != 0)
{
char tmpbuf[KERNEL_CONFIG_MAX_LINE_WIDTH];
const char *tegra_config = KERNEL_CONFIG_TEGRA_MAGIC;
const char *tegra2_config = KERNEL_CONFIG_TEGRA2_MAGIC;
const char *tegra3_config = KERNEL_CONFIG_TEGRA3_MAGIC;
const char *tegra4_config = KERNEL_CONFIG_TEGRA4_MAGIC;
int len = strlen(tegra_config);
int len2 = strlen(tegra2_config);
int len3 = strlen(tegra3_config);
int len4 = strlen(tegra4_config);
while (0 != gzgets(kernelConfig, tmpbuf, KERNEL_CONFIG_MAX_LINE_WIDTH))
{
if (0 == strncmp(tmpbuf, tegra_config, len))
{
result = 1;
}
if (0 == strncmp(tmpbuf, tegra2_config, len2))
{
result = 2;
break;
}
if (0 == strncmp(tmpbuf, tegra3_config, len3))
{
result = 3;
break;
}
if (0 == strncmp(tmpbuf, tegra4_config, len4))
{
result = 4;
break;
}
}
gzclose(kernelConfig);
}
else
{
result = TEGRA_DETECTOR_ERROR;
}
return result;
}
@@ -0,0 +1,9 @@
#ifndef __TEGRA_DETECTOR_H__
#define __TEGRA_DETECTOR_H__
#define TEGRA_DETECTOR_ERROR -2
#define TEGRA_NOT_TEGRA -1
int DetectTegra();
#endif