2012-06-21 22:50:05 +08:00
|
|
|
#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();
|
2013-02-25 19:34:06 +08:00
|
|
|
const int OpenCVEngine::KnownVersions[] = {2040000, 2040100, 2040200, 2040300, 2040301, 2040302, 2040400};
|
2012-06-21 22:50:05 +08:00
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
bool OpenCVEngine::ValidateVersion(int version)
|
2012-06-21 22:50:05 +08:00
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
for (size_t i = 0; i < sizeof(KnownVersions)/sizeof(int); i++)
|
|
|
|
if (KnownVersions[i] == version)
|
|
|
|
return true;
|
2012-06-21 22:50:05 +08:00
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
return false;
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
int OpenCVEngine::NormalizeVersionString(std::string version)
|
2012-06-21 22:50:05 +08:00
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
int result = 0;
|
2012-06-21 22:50:05 +08:00
|
|
|
|
|
|
|
if (version.empty())
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
return result;
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> parts = SplitStringVector(version, '.');
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
// 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)
|
2012-06-21 22:50:05 +08:00
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
int digit = atoi(it->c_str());
|
|
|
|
result += multiplyer*digit;
|
|
|
|
multiplyer /= 100;
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
if (!ValidateVersion(result))
|
|
|
|
result = 0;
|
|
|
|
|
2012-06-21 22:50:05 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenCVEngine::OpenCVEngine(IPackageManager* PkgManager):
|
|
|
|
PackageManager(PkgManager)
|
|
|
|
{
|
|
|
|
assert(PkgManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t OpenCVEngine::GetVersion()
|
|
|
|
{
|
2012-10-02 19:02:04 +08:00
|
|
|
return OPEN_CV_ENGINE_VERSION;
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String16 OpenCVEngine::GetLibPathByVersion(android::String16 version)
|
|
|
|
{
|
|
|
|
std::string std_version(String8(version).string());
|
2013-01-16 20:47:06 +08:00
|
|
|
int norm_version;
|
2012-06-21 22:50:05 +08:00
|
|
|
std::string path;
|
|
|
|
|
|
|
|
LOGD("OpenCVEngine::GetLibPathByVersion(%s) impl", String8(version).string());
|
|
|
|
|
|
|
|
norm_version = NormalizeVersionString(std_version);
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
if (0 != norm_version)
|
2012-06-21 22:50:05 +08:00
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
path = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
|
|
|
|
if (path.empty())
|
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", String8(version).string(), norm_version);
|
2012-10-24 16:47:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FixPermissions(path);
|
|
|
|
}
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
LOGE("OpenCV version \"%s\" (%d) is not supported", String8(version).string(), norm_version);
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return String16(path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
android::String16 OpenCVEngine::GetLibraryList(android::String16 version)
|
|
|
|
{
|
|
|
|
std::string std_version = String8(version).string();
|
2013-01-16 20:47:06 +08:00
|
|
|
int norm_version;
|
2012-06-21 22:50:05 +08:00
|
|
|
String16 result;
|
|
|
|
norm_version = NormalizeVersionString(std_version);
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
if (0 != norm_version)
|
2012-06-21 22:50:05 +08:00
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
std::string tmp = PackageManager->GetPackagePathByVersion(norm_version, Platform, CpuID);
|
|
|
|
if (!tmp.empty())
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
tmp += (std::string("/") + LIB_OPENCV_INFO_NAME);
|
|
|
|
|
|
|
|
LOGD("Trying to load info library \"%s\"", tmp.c_str());
|
|
|
|
|
|
|
|
void* handle;
|
2012-12-20 16:28:30 +08:00
|
|
|
InfoFunctionType info_func;
|
2012-10-24 16:47:43 +08:00
|
|
|
|
|
|
|
handle = dlopen(tmp.c_str(), RTLD_LAZY);
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
const char* error;
|
|
|
|
|
|
|
|
dlerror();
|
2012-12-20 16:28:30 +08:00
|
|
|
info_func = (InfoFunctionType)dlsym(handle, "GetLibraryList");
|
2012-10-24 16:47:43 +08:00
|
|
|
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");
|
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
LOGI("Package OpenCV of version \"%s\" (%d) is not installed. Try to install it :)", std_version.c_str(), norm_version);
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
LOGE("OpenCV version \"%s\" is not supported", std_version.c_str());
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenCVEngine::InstallVersion(android::String16 version)
|
|
|
|
{
|
|
|
|
std::string std_version = String8(version).string();
|
2013-01-16 20:47:06 +08:00
|
|
|
int norm_version;
|
2012-06-21 22:50:05 +08:00
|
|
|
bool result = false;
|
|
|
|
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGD("OpenCVEngine::InstallVersion() begin");
|
|
|
|
|
2012-06-21 22:50:05 +08:00
|
|
|
norm_version = NormalizeVersionString(std_version);
|
|
|
|
|
2013-01-16 20:47:06 +08:00
|
|
|
if (0 != norm_version)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
LOGD("PackageManager->InstallVersion call");
|
|
|
|
result = PackageManager->InstallVersion(norm_version, Platform, CpuID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-16 20:47:06 +08:00
|
|
|
LOGE("OpenCV version \"%s\" (%d) is not supported", std_version.c_str(), norm_version);
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
LOGD("Fixing permissions error");
|
|
|
|
return false;
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dirent* files = readdir(dir);
|
|
|
|
while (files)
|
|
|
|
{
|
2012-10-24 16:47:43 +08:00
|
|
|
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);
|
2012-06-21 22:50:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|