rsCpuExecutable.h revision 1de97c307624f57d95281ebaa77c2129e66e21bc
1/*
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
32    static bool createSharedLibrary(const char* driverName,
33                                    const char* cacheDir,
34                                    const char* resName);
35#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,
46                                   const char *nativeLibDir = nullptr);
47
48private:
49    // Attempt to load the shared library from origName, but then fall back to
50    // creating a copy of the shared library if necessary (to ensure instancing).
51    // This function returns the dlopen()-ed handle if successful.
52    static void *loadSOHelper(const char *origName, const char *cacheDir,
53                              const char *resName);
54
55    static const char* LD_EXE_PATH;
56    static const char* RS_CACHE_DIR;
57};
58
59class ScriptExecutable {
60public:
61    ScriptExecutable(Context* RSContext,
62                     void** fieldAddress, bool* fieldIsObject,
63                     const char* const * fieldName, size_t varCount,
64                     InvokeFunc_t* invokeFunctions, size_t funcCount,
65                     ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures,
66                     size_t forEachCount,
67                     const char** pragmaKeys, const char** pragmaValues,
68                     size_t pragmaCount,
69                     const char **globalNames, const void **globalAddresses,
70                     const size_t *globalSizes, size_t globalEntries,
71                     bool isThreadable, uint32_t buildChecksum) :
72        mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
73        mFieldName(fieldName), mExportedVarCount(varCount),
74        mInvokeFunctions(invokeFunctions), mFuncCount(funcCount),
75        mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
76        mForEachCount(forEachCount),
77        mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
78        mPragmaCount(pragmaCount), mGlobalNames(globalNames),
79        mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes),
80        mGlobalEntries(globalEntries), mIsThreadable(isThreadable),
81        mBuildChecksum(buildChecksum), mRS(RSContext) {
82    }
83
84    ~ScriptExecutable() {
85        for (size_t i = 0; i < mExportedVarCount; ++i) {
86            if (mFieldIsObject[i]) {
87                if (mFieldAddress[i] != nullptr) {
88                    rs_object_base *obj_addr =
89                            reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
90                    rsrClearObject(mRS, obj_addr);
91                }
92            }
93        }
94
95        for (size_t i = 0; i < mPragmaCount; ++i) {
96            delete [] mPragmaKeys[i];
97            delete [] mPragmaValues[i];
98        }
99        delete[] mPragmaValues;
100        delete[] mPragmaKeys;
101
102        delete[] mForEachSignatures;
103        delete[] mForEachFunctions;
104
105        delete[] mInvokeFunctions;
106
107        for (size_t i = 0; i < mExportedVarCount; i++) {
108            delete[] mFieldName[i];
109        }
110        delete[] mFieldName;
111        delete[] mFieldIsObject;
112        delete[] mFieldAddress;
113    }
114
115    // Create an ScriptExecutable object from a shared object.
116    // If expectedChecksum is not zero, it will be compared to the checksum
117    // embedded in the shared object. A mismatch will cause a failure.
118    // If succeeded, returns the new object. Otherwise, returns nullptr.
119    static ScriptExecutable*
120            createFromSharedObject(Context* RSContext, void* sharedObj,
121                                   uint32_t expectedChecksum = 0);
122
123    size_t getExportedVariableCount() const { return mExportedVarCount; }
124    size_t getExportedFunctionCount() const { return mFuncCount; }
125    size_t getExportedForEachCount() const { return mForEachCount; }
126    size_t getPragmaCount() const { return mPragmaCount; }
127
128    void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
129    void* getFieldAddress(const char* name) const;
130    bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
131    const char* getFieldName(int slot) const { return mFieldName[slot]; }
132
133    InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
134
135    ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
136    uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
137
138    const char ** getPragmaKeys() const { return mPragmaKeys; }
139    const char ** getPragmaValues() const { return mPragmaValues; }
140
141    const char* getGlobalName(int i) const {
142        if (i < mGlobalEntries) {
143            return mGlobalNames[i];
144        } else {
145            return nullptr;
146        }
147    }
148    const void* getGlobalAddress(int i) const {
149        if (i < mGlobalEntries) {
150            return mGlobalAddresses[i];
151        } else {
152            return nullptr;
153        }
154    }
155    size_t getGlobalSize(int i) const {
156        if (i < mGlobalEntries) {
157            return mGlobalSizes[i];
158        } else {
159            return 0;
160        }
161    }
162    int getGlobalEntries() const { return mGlobalEntries; }
163
164    bool getThreadable() const { return mIsThreadable; }
165
166    uint32_t getBuildChecksum() const { return mBuildChecksum; }
167
168    bool dumpGlobalInfo() const;
169
170private:
171    void** mFieldAddress;
172    bool* mFieldIsObject;
173    const char* const * mFieldName;
174    size_t mExportedVarCount;
175
176    InvokeFunc_t* mInvokeFunctions;
177    size_t mFuncCount;
178
179    ForEachFunc_t* mForEachFunctions;
180    uint32_t* mForEachSignatures;
181    size_t mForEachCount;
182
183    const char ** mPragmaKeys;
184    const char ** mPragmaValues;
185    size_t mPragmaCount;
186
187    const char ** mGlobalNames;
188    const void ** mGlobalAddresses;
189    const size_t * mGlobalSizes;
190    int mGlobalEntries;
191
192    bool mIsThreadable;
193    uint32_t mBuildChecksum;
194
195    Context* mRS;
196};
197
198}  // namespace renderscript
199}  // namespace android
200
201#endif  // ANDROID_RENDERSCRIPT_EXECUTABLE_H
202