blob: 72c352c247ac1796031728b4986f79e7ee83283b [file] [log] [blame]
Yang Ni2abfcc62015-02-17 16:05:19 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef ANDROID_RENDERSCRIPT_EXECUTABLE_H
18#define ANDROID_RENDERSCRIPT_EXECUTABLE_H
19
20#include <stdlib.h>
21
22#include "rsCpuScript.h"
23
24namespace android {
25namespace renderscript {
26
27class Context;
28
29class SharedLibraryUtils {
30public:
31#ifndef RS_COMPATIBILITY_LIB
Stephen Hines4c368af2015-05-06 00:43:02 -070032 static bool createSharedLibrary(const char* driverName,
33 const char* cacheDir,
34 const char* resName);
Yang Ni2abfcc62015-02-17 16:05:19 -080035#endif
36
37 // Load the shared library referred to by cacheDir and resName. If we have
38 // already loaded this library, we instead create a new copy (in the
39 // cache dir) and then load that. We then immediately destroy the copy.
40 // This is required behavior to implement script instancing for the support
41 // library, since shared objects are loaded and de-duped by name only.
42
43 // For 64bit RS Support Lib, the shared lib path cannot be constructed from
44 // cacheDir, so nativeLibDir is needed to load shared libs.
45 static void* loadSharedLibrary(const char *cacheDir, const char *resName,
Yang Ni1efae292015-06-27 15:45:18 -070046 const char *nativeLibDir = nullptr,
47 bool *alreadyLoaded = nullptr);
48
49 // Create a len length string containing random characters from [A-Za-z0-9].
50 static String8 getRandomString(size_t len);
Yang Ni2abfcc62015-02-17 16:05:19 -080051
52private:
53 // Attempt to load the shared library from origName, but then fall back to
54 // creating a copy of the shared library if necessary (to ensure instancing).
55 // This function returns the dlopen()-ed handle if successful.
56 static void *loadSOHelper(const char *origName, const char *cacheDir,
Yang Ni1efae292015-06-27 15:45:18 -070057 const char *resName, bool* alreadyLoaded = nullptr);
Yang Ni2abfcc62015-02-17 16:05:19 -080058
59 static const char* LD_EXE_PATH;
60 static const char* RS_CACHE_DIR;
61};
62
63class ScriptExecutable {
64public:
Yang Ni5e480022016-04-06 09:34:34 -070065 ScriptExecutable(void** fieldAddress, bool* fieldIsObject,
Yang Ni062c2872015-02-20 15:20:00 -080066 const char* const * fieldName, size_t varCount,
Yang Ni2abfcc62015-02-17 16:05:19 -080067 InvokeFunc_t* invokeFunctions, size_t funcCount,
68 ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures,
69 size_t forEachCount,
Matt Wala14ce0072015-07-30 17:30:25 -070070 ReduceFunc_t* reduceFunctions, size_t reduceCount,
David Gross6c1876b2016-01-15 11:52:14 -080071 ReduceNewDescription *reduceNewDescriptions, size_t reduceNewCount,
Yang Ni062c2872015-02-20 15:20:00 -080072 const char** pragmaKeys, const char** pragmaValues,
Yang Ni2abfcc62015-02-17 16:05:19 -080073 size_t pragmaCount,
Stephen Hines8409d642015-04-28 18:49:56 -070074 const char **globalNames, const void **globalAddresses,
Stephen Hines5aa018c2015-05-20 18:09:57 -070075 const size_t *globalSizes,
76 const uint32_t *globalProperties, size_t globalEntries,
Yang Nicb170152015-04-16 10:27:02 -070077 bool isThreadable, uint32_t buildChecksum) :
Yang Ni2abfcc62015-02-17 16:05:19 -080078 mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
Yang Ni062c2872015-02-20 15:20:00 -080079 mFieldName(fieldName), mExportedVarCount(varCount),
80 mInvokeFunctions(invokeFunctions), mFuncCount(funcCount),
81 mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
82 mForEachCount(forEachCount),
Matt Wala14ce0072015-07-30 17:30:25 -070083 mReduceFunctions(reduceFunctions), mReduceCount(reduceCount),
David Gross6c1876b2016-01-15 11:52:14 -080084 mReduceNewDescriptions(reduceNewDescriptions), mReduceNewCount(reduceNewCount),
Yang Ni062c2872015-02-20 15:20:00 -080085 mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
Stephen Hines8409d642015-04-28 18:49:56 -070086 mPragmaCount(pragmaCount), mGlobalNames(globalNames),
87 mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes),
Stephen Hines5aa018c2015-05-20 18:09:57 -070088 mGlobalProperties(globalProperties), mGlobalEntries(globalEntries),
Yang Ni5e480022016-04-06 09:34:34 -070089 mIsThreadable(isThreadable), mBuildChecksum(buildChecksum) {
Yang Ni2abfcc62015-02-17 16:05:19 -080090 }
91
92 ~ScriptExecutable() {
93 for (size_t i = 0; i < mExportedVarCount; ++i) {
94 if (mFieldIsObject[i]) {
95 if (mFieldAddress[i] != nullptr) {
96 rs_object_base *obj_addr =
97 reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
Yang Ni5e480022016-04-06 09:34:34 -070098 rsrClearObject(obj_addr);
Yang Ni2abfcc62015-02-17 16:05:19 -080099 }
100 }
101 }
102
103 for (size_t i = 0; i < mPragmaCount; ++i) {
104 delete [] mPragmaKeys[i];
105 delete [] mPragmaValues[i];
106 }
Yang Ni2abfcc62015-02-17 16:05:19 -0800107 delete[] mPragmaValues;
108 delete[] mPragmaKeys;
Yang Ni062c2872015-02-20 15:20:00 -0800109
Matt Wala14ce0072015-07-30 17:30:25 -0700110 delete[] mReduceFunctions;
111
David Gross6c1876b2016-01-15 11:52:14 -0800112 delete[] mReduceNewDescriptions;
113
Yang Ni2abfcc62015-02-17 16:05:19 -0800114 delete[] mForEachSignatures;
115 delete[] mForEachFunctions;
Yang Ni062c2872015-02-20 15:20:00 -0800116
Yang Ni2abfcc62015-02-17 16:05:19 -0800117 delete[] mInvokeFunctions;
Yang Ni062c2872015-02-20 15:20:00 -0800118
119 for (size_t i = 0; i < mExportedVarCount; i++) {
120 delete[] mFieldName[i];
121 }
122 delete[] mFieldName;
Yang Ni2abfcc62015-02-17 16:05:19 -0800123 delete[] mFieldIsObject;
124 delete[] mFieldAddress;
125 }
126
Yang Nicb170152015-04-16 10:27:02 -0700127 // Create an ScriptExecutable object from a shared object.
128 // If expectedChecksum is not zero, it will be compared to the checksum
129 // embedded in the shared object. A mismatch will cause a failure.
130 // If succeeded, returns the new object. Otherwise, returns nullptr.
Yang Ni2abfcc62015-02-17 16:05:19 -0800131 static ScriptExecutable*
Yang Ni5e480022016-04-06 09:34:34 -0700132 createFromSharedObject(void* sharedObj,
Yang Nicb170152015-04-16 10:27:02 -0700133 uint32_t expectedChecksum = 0);
Yang Ni2abfcc62015-02-17 16:05:19 -0800134
135 size_t getExportedVariableCount() const { return mExportedVarCount; }
136 size_t getExportedFunctionCount() const { return mFuncCount; }
137 size_t getExportedForEachCount() const { return mForEachCount; }
Matt Wala14ce0072015-07-30 17:30:25 -0700138 size_t getExportedReduceCount() const { return mReduceCount; }
David Gross6c1876b2016-01-15 11:52:14 -0800139 size_t getExportedReduceNewCount() const { return mReduceNewCount; }
Yang Ni2abfcc62015-02-17 16:05:19 -0800140 size_t getPragmaCount() const { return mPragmaCount; }
141
142 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
Yang Ni062c2872015-02-20 15:20:00 -0800143 void* getFieldAddress(const char* name) const;
Yang Ni2abfcc62015-02-17 16:05:19 -0800144 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
Yang Ni062c2872015-02-20 15:20:00 -0800145 const char* getFieldName(int slot) const { return mFieldName[slot]; }
146
Yang Ni2abfcc62015-02-17 16:05:19 -0800147 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
Yang Ni062c2872015-02-20 15:20:00 -0800148
Yang Ni2abfcc62015-02-17 16:05:19 -0800149 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
150 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
151
Matt Wala14ce0072015-07-30 17:30:25 -0700152 ReduceFunc_t getReduceFunction(int slot) const { return mReduceFunctions[slot]; }
153
David Gross6c1876b2016-01-15 11:52:14 -0800154 const ReduceNewDescription* getReduceNewDescription(int slot) const {
155 return &mReduceNewDescriptions[slot];
156 }
157
Yang Ni2abfcc62015-02-17 16:05:19 -0800158 const char ** getPragmaKeys() const { return mPragmaKeys; }
159 const char ** getPragmaValues() const { return mPragmaValues; }
160
Stephen Hines8409d642015-04-28 18:49:56 -0700161 const char* getGlobalName(int i) const {
162 if (i < mGlobalEntries) {
163 return mGlobalNames[i];
164 } else {
165 return nullptr;
166 }
167 }
168 const void* getGlobalAddress(int i) const {
169 if (i < mGlobalEntries) {
170 return mGlobalAddresses[i];
171 } else {
172 return nullptr;
173 }
174 }
175 size_t getGlobalSize(int i) const {
176 if (i < mGlobalEntries) {
177 return mGlobalSizes[i];
178 } else {
179 return 0;
180 }
181 }
Stephen Hines5aa018c2015-05-20 18:09:57 -0700182 uint32_t getGlobalProperties(int i) const {
183 if (i < mGlobalEntries) {
184 return mGlobalProperties[i];
185 } else {
186 return 0;
187 }
188 }
Stephen Hines8409d642015-04-28 18:49:56 -0700189 int getGlobalEntries() const { return mGlobalEntries; }
190
Yang Ni2abfcc62015-02-17 16:05:19 -0800191 bool getThreadable() const { return mIsThreadable; }
192
Yang Nicb170152015-04-16 10:27:02 -0700193 uint32_t getBuildChecksum() const { return mBuildChecksum; }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800194
Stephen Hines8409d642015-04-28 18:49:56 -0700195 bool dumpGlobalInfo() const;
196
Yang Ni2abfcc62015-02-17 16:05:19 -0800197private:
198 void** mFieldAddress;
199 bool* mFieldIsObject;
Yang Ni062c2872015-02-20 15:20:00 -0800200 const char* const * mFieldName;
Yang Ni2abfcc62015-02-17 16:05:19 -0800201 size_t mExportedVarCount;
202
203 InvokeFunc_t* mInvokeFunctions;
204 size_t mFuncCount;
205
206 ForEachFunc_t* mForEachFunctions;
207 uint32_t* mForEachSignatures;
208 size_t mForEachCount;
209
Matt Wala14ce0072015-07-30 17:30:25 -0700210 ReduceFunc_t* mReduceFunctions;
211 size_t mReduceCount;
212
David Gross6c1876b2016-01-15 11:52:14 -0800213 ReduceNewDescription* mReduceNewDescriptions;
214 size_t mReduceNewCount;
215
Yang Ni2abfcc62015-02-17 16:05:19 -0800216 const char ** mPragmaKeys;
217 const char ** mPragmaValues;
218 size_t mPragmaCount;
219
Stephen Hines8409d642015-04-28 18:49:56 -0700220 const char ** mGlobalNames;
221 const void ** mGlobalAddresses;
222 const size_t * mGlobalSizes;
Stephen Hines5aa018c2015-05-20 18:09:57 -0700223 const uint32_t * mGlobalProperties;
Stephen Hines8409d642015-04-28 18:49:56 -0700224 int mGlobalEntries;
225
Yang Ni2abfcc62015-02-17 16:05:19 -0800226 bool mIsThreadable;
Yang Nicb170152015-04-16 10:27:02 -0700227 uint32_t mBuildChecksum;
Yang Ni2abfcc62015-02-17 16:05:19 -0800228};
229
230} // namespace renderscript
231} // namespace android
232
233#endif // ANDROID_RENDERSCRIPT_EXECUTABLE_H