summaryrefslogtreecommitdiff
path: root/cpu_ref/rsCpuScript.cpp
diff options
context:
space:
mode:
authorYang Ni <yangni@google.com>2015-01-20 15:31:15 -0800
committerYang Ni <yangni@google.com>2015-01-21 17:30:31 -0800
commitd9bae689c1b8c3f2ed1a5f2b374dc9393584b8dd (patch)
tree29b41f90687b70461fb64bded2453e1d6a6c72fa /cpu_ref/rsCpuScript.cpp
parentcdf6d210efdd014d9681b03ffbf4d850c229f680 (diff)
Created a new class to represent executable.
The new class represents executables created from shared objects, which allows query exported fields, for each, etc. This allows this functionality to be shared with other part of the system besides CPU script, e.g., script groups. Change-Id: I5223c329bdb70085eb1dce7aab5ec91626f579f4
Diffstat (limited to 'cpu_ref/rsCpuScript.cpp')
-rw-r--r--cpu_ref/rsCpuScript.cpp284
1 files changed, 112 insertions, 172 deletions
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index 38a0bf57..7062cb48 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -469,12 +469,6 @@ RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
mScriptSO = nullptr;
- mInvokeFunctions = nullptr;
- mForEachFunctions = nullptr;
- mFieldAddress = nullptr;
- mFieldIsObject = nullptr;
- mForEachSignatures = nullptr;
-
#ifndef RS_COMPATIBILITY_LIB
mCompilerDriver = nullptr;
#endif
@@ -484,7 +478,7 @@ RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
mRootExpand = nullptr;
mInit = nullptr;
mFreeChildren = nullptr;
-
+ mScriptExec = nullptr;
mBoundAllocs = nullptr;
mIntrinsicData = nullptr;
@@ -492,12 +486,6 @@ RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
}
bool RsdCpuScriptImpl::storeRSInfoFromSO() {
- char line[MAXLINE];
- size_t varCount = 0;
- size_t funcCount = 0;
- size_t forEachCount = 0;
- size_t objectSlotCount = 0;
-
mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
if (mRoot) {
//ALOGE("Found root(): %p", mRoot);
@@ -515,186 +503,154 @@ bool RsdCpuScriptImpl::storeRSInfoFromSO() {
//ALOGE("Found .rs.dtor(): %p", mFreeChildren);
}
- const char *rsInfo = (const char *) dlsym(mScriptSO, ".rs.info");
- if (rsInfo) {
- //ALOGE("Found .rs.info(): %p - %s", rsInfo, rsInfo);
+ mScriptExec = ScriptExecutable::createFromSharedObject(
+ mCtx->getContext(), mScriptSO);
+
+ if (mScriptExec == nullptr) {
+ return false;
+ }
+
+ size_t varCount = mScriptExec->getExportedVariableCount();
+ if (varCount > 0) {
+ mBoundAllocs = new Allocation *[varCount];
+ memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
}
+ return true;
+}
+
+ScriptExecutable* ScriptExecutable::createFromSharedObject(
+ Context* RSContext, void* sharedObj) {
+ char line[MAXLINE];
+
+ size_t varCount = 0;
+ size_t funcCount = 0;
+ size_t forEachCount = 0;
+ size_t objectSlotCount = 0;
+
+ const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
+
if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
+ return nullptr;
}
if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
ALOGE("Invalid export var count!: %s", line);
- goto error;
+ return nullptr;
}
- mExportedVariableCount = varCount;
- //ALOGE("varCount: %zu", varCount);
- if (varCount > 0) {
- // Start by creating/zeroing this member, since we don't want to
- // accidentally clean up invalid pointers later (if we error out).
- mFieldIsObject = new bool[varCount];
- if (mFieldIsObject == nullptr) {
- goto error;
+ std::vector<void*> fieldAddress;
+
+ for (size_t i = 0; i < varCount; ++i) {
+ if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
+ return nullptr;
}
- memset(mFieldIsObject, 0, varCount * sizeof(*mFieldIsObject));
- mFieldAddress = new void*[varCount];
- if (mFieldAddress == nullptr) {
- goto error;
+ char *c = strrchr(line, '\n');
+ if (c) {
+ *c = '\0';
}
- for (size_t i = 0; i < varCount; ++i) {
- if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
- }
- char *c = strrchr(line, '\n');
- if (c) {
- *c = '\0';
- }
- mFieldAddress[i] = dlsym(mScriptSO, line);
- if (mFieldAddress[i] == nullptr) {
- ALOGE("Failed to find variable address for %s: %s",
- line, dlerror());
- // Not a critical error if we don't find a global variable.
- }
- else {
- //ALOGE("Found variable %s at %p", line,
- //mFieldAddress[i]);
- }
+ void* addr = dlsym(sharedObj, line);
+ if (addr == nullptr) {
+ ALOGE("Failed to find variable address for %s: %s",
+ line, dlerror());
+ // Not a critical error if we don't find a global variable.
}
+ fieldAddress.push_back(addr);
}
if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
+ return nullptr;
}
if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
ALOGE("Invalid export func count!: %s", line);
- goto error;
+ return nullptr;
}
- mExportedFunctionCount = funcCount;
- //ALOGE("funcCount: %zu", funcCount);
+ std::vector<InvokeFunc_t> invokeFunctions(funcCount);
- if (funcCount > 0) {
- mInvokeFunctions = new InvokeFunc_t[funcCount];
- if (mInvokeFunctions == nullptr) {
- goto error;
+ for (size_t i = 0; i < funcCount; ++i) {
+ if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
+ return nullptr ;
+ }
+ char *c = strrchr(line, '\n');
+ if (c) {
+ *c = '\0';
}
- for (size_t i = 0; i < funcCount; ++i) {
- if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
- }
- char *c = strrchr(line, '\n');
- if (c) {
- *c = '\0';
- }
- mInvokeFunctions[i] = (InvokeFunc_t) dlsym(mScriptSO, line);
- if (mInvokeFunctions[i] == nullptr) {
- ALOGE("Failed to get function address for %s(): %s",
- line, dlerror());
- goto error;
- }
- else {
- //ALOGE("Found InvokeFunc_t %s at %p", line, mInvokeFunctions[i]);
- }
+ invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line);
+ if (invokeFunctions[i] == nullptr) {
+ ALOGE("Failed to get function address for %s(): %s",
+ line, dlerror());
+ return nullptr;
}
}
if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
+ return nullptr;
}
if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
ALOGE("Invalid export forEach count!: %s", line);
- goto error;
+ return nullptr;
}
- if (forEachCount > 0) {
+ std::vector<ForEachFunc_t> forEachFunctions(forEachCount);
+ std::vector<uint32_t> forEachSignatures(forEachCount);
+
+ for (size_t i = 0; i < forEachCount; ++i) {
+ unsigned int tmpSig = 0;
+ char tmpName[MAXLINE];
- mForEachSignatures = new uint32_t[forEachCount];
- if (mForEachSignatures == nullptr) {
- goto error;
+ if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
+ return nullptr;
}
- mForEachFunctions = new ForEachFunc_t[forEachCount];
- if (mForEachFunctions == nullptr) {
- goto error;
+ if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
+ &tmpSig, tmpName) != 2) {
+ ALOGE("Invalid export forEach!: %s", line);
+ return nullptr;
}
- for (size_t i = 0; i < forEachCount; ++i) {
- unsigned int tmpSig = 0;
- char tmpName[MAXLINE];
- if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
- }
- if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
- &tmpSig, tmpName) != 2) {
- ALOGE("Invalid export forEach!: %s", line);
- goto error;
- }
-
- // Lookup the expanded ForEach kernel.
- strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
- mForEachSignatures[i] = tmpSig;
- mForEachFunctions[i] =
- (ForEachFunc_t) dlsym(mScriptSO, tmpName);
- if (i != 0 && mForEachFunctions[i] == nullptr) {
- // Ignore missing root.expand functions.
- // root() is always specified at location 0.
- ALOGE("Failed to find forEach function address for %s: %s",
- tmpName, dlerror());
- goto error;
- }
- else {
- //ALOGE("Found forEach %s at %p", tmpName, mForEachFunctions[i]);
- }
+ // Lookup the expanded ForEach kernel.
+ strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
+ forEachSignatures[i] = tmpSig;
+ forEachFunctions[i] =
+ (ForEachFunc_t) dlsym(sharedObj, tmpName);
+ if (i != 0 && forEachFunctions[i] == nullptr) {
+ // Ignore missing root.expand functions.
+ // root() is always specified at location 0.
+ ALOGE("Failed to find forEach function address for %s: %s",
+ tmpName, dlerror());
+ return nullptr;
}
}
if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
+ return nullptr;
}
if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
ALOGE("Invalid object slot count!: %s", line);
- goto error;
+ return nullptr;
}
- if (objectSlotCount > 0) {
- rsAssert(varCount > 0);
- for (size_t i = 0; i < objectSlotCount; ++i) {
- uint32_t varNum = 0;
- if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
- goto error;
- }
- if (sscanf(line, "%u", &varNum) != 1) {
- ALOGE("Invalid object slot!: %s", line);
- goto error;
- }
+ std::vector<bool> fieldIsObject(varCount, false);
- if (varNum < varCount) {
- mFieldIsObject[varNum] = true;
- }
+ rsAssert(varCount > 0);
+ for (size_t i = 0; i < objectSlotCount; ++i) {
+ uint32_t varNum = 0;
+ if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
+ return nullptr;
+ }
+ if (sscanf(line, "%u", &varNum) != 1) {
+ ALOGE("Invalid object slot!: %s", line);
+ return nullptr;
}
- }
-
- if (varCount > 0) {
- mBoundAllocs = new Allocation *[varCount];
- memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
- }
- if (mScriptSO == (void*)1) {
- //rsdLookupRuntimeStub(script, "acos");
+ if (varNum < varCount) {
+ fieldIsObject[varNum] = true;
+ }
}
- return true;
-
-error:
- delete[] mInvokeFunctions;
- delete[] mForEachFunctions;
- delete[] mFieldAddress;
- delete[] mFieldIsObject;
- delete[] mForEachSignatures;
- delete[] mBoundAllocs;
-
- return false;
+ return new ScriptExecutable(
+ RSContext, fieldAddress, fieldIsObject, invokeFunctions,
+ forEachFunctions, forEachSignatures);
}
bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
@@ -851,8 +807,8 @@ const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, c
void RsdCpuScriptImpl::populateScript(Script *script) {
// Copy info over to runtime
- script->mHal.info.exportedFunctionCount = mExportedFunctionCount;
- script->mHal.info.exportedVariableCount = mExportedVariableCount;
+ script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
+ script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
script->mHal.info.exportedPragmaCount = 0;
script->mHal.info.exportedPragmaKeyList = 0;
script->mHal.info.exportedPragmaValueList = 0;
@@ -1025,9 +981,9 @@ void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
mtls->script = this;
mtls->fep.slot = slot;
- mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]);
+ mtls->kernel = mScriptExec->getForEachFunction(slot);
rsAssert(mtls->kernel != nullptr);
- mtls->sig = mForEachSignatures[slot];
+ mtls->sig = mScriptExec->getForEachSignature(slot);
}
int RsdCpuScriptImpl::invokeRoot() {
@@ -1070,7 +1026,7 @@ void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
reinterpret_cast<void (*)(const void *, uint32_t)>(
- mInvokeFunctions[slot])(ap? (const void *) ap: params, paramLength);
+ mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
mCtx->setTLS(oldTLS);
}
@@ -1084,7 +1040,7 @@ void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t data
//return;
//}
- int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
+ int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
if (!destPtr) {
//ALOGV("Calling setVar on slot = %i which is null", slot);
return;
@@ -1097,7 +1053,7 @@ void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength
//rsAssert(!script->mFieldIsObject[slot]);
//ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
- int32_t *srcPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
+ int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
if (!srcPtr) {
//ALOGV("Calling setVar on slot = %i which is null", slot);
return;
@@ -1109,7 +1065,7 @@ void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength
void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
const Element *elem,
const uint32_t *dims, size_t dimLength) {
- int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
+ int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
if (!destPtr) {
//ALOGV("Calling setVar on slot = %i which is null", slot);
return;
@@ -1146,7 +1102,7 @@ void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
//rsAssert(!script->mFieldIsObject[slot]);
//ALOGE("setGlobalBind %i %p", slot, data);
- int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
+ int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
if (!destPtr) {
//ALOGV("Calling setVar on slot = %i which is null", slot);
return;
@@ -1165,7 +1121,7 @@ void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
//rsAssert(script->mFieldIsObject[slot]);
//ALOGE("setGlobalObj %i %p", slot, data);
- int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
+ int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
if (!destPtr) {
//ALOGV("Calling setVar on slot = %i which is null", slot);
return;
@@ -1176,30 +1132,14 @@ void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
RsdCpuScriptImpl::~RsdCpuScriptImpl() {
#ifndef RS_COMPATIBILITY_LIB
-
if (mCompilerDriver) {
delete mCompilerDriver;
}
-
#endif
- if (mFieldIsObject) {
- for (size_t i = 0; i < mExportedVariableCount; ++i) {
- if (mFieldIsObject[i]) {
- if (mFieldAddress[i] != nullptr) {
- rs_object_base *obj_addr =
- reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
- rsrClearObject(mCtx->getContext(), obj_addr);
- }
- }
- }
+ if (mScriptExec != nullptr) {
+ delete mScriptExec;
}
-
- if (mInvokeFunctions) delete[] mInvokeFunctions;
- if (mForEachFunctions) delete[] mForEachFunctions;
- if (mFieldAddress) delete[] mFieldAddress;
- if (mFieldIsObject) delete[] mFieldIsObject;
- if (mForEachSignatures) delete[] mForEachSignatures;
if (mBoundAllocs) delete[] mBoundAllocs;
if (mScriptSO) {
dlclose(mScriptSO);