rsCpuExecutable.h revision 062c287f573ecc06c38ee4295e5627e12c52ac3d
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* cacheDir, const char* resName);
33#endif
34
35    // Load the shared library referred to by cacheDir and resName. If we have
36    // already loaded this library, we instead create a new copy (in the
37    // cache dir) and then load that. We then immediately destroy the copy.
38    // This is required behavior to implement script instancing for the support
39    // library, since shared objects are loaded and de-duped by name only.
40
41    // For 64bit RS Support Lib, the shared lib path cannot be constructed from
42    // cacheDir, so nativeLibDir is needed to load shared libs.
43    static void* loadSharedLibrary(const char *cacheDir, const char *resName,
44                                   const char *nativeLibDir = nullptr);
45
46private:
47    // Attempt to load the shared library from origName, but then fall back to
48    // creating a copy of the shared library if necessary (to ensure instancing).
49    // This function returns the dlopen()-ed handle if successful.
50    static void *loadSOHelper(const char *origName, const char *cacheDir,
51                              const char *resName);
52
53    static const char* LD_EXE_PATH;
54    static const char* RS_CACHE_DIR;
55};
56
57class ScriptExecutable {
58public:
59    ScriptExecutable(Context* RSContext,
60                     void** fieldAddress, bool* fieldIsObject,
61                     const char* const * fieldName, size_t varCount,
62                     InvokeFunc_t* invokeFunctions, size_t funcCount,
63                     ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures,
64                     size_t forEachCount,
65                     const char** pragmaKeys, const char** pragmaValues,
66                     size_t pragmaCount,
67                     bool isThreadable, const char *buildChecksum) :
68        mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
69        mFieldName(fieldName), mExportedVarCount(varCount),
70        mInvokeFunctions(invokeFunctions), mFuncCount(funcCount),
71        mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
72        mForEachCount(forEachCount),
73        mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
74        mPragmaCount(pragmaCount), mIsThreadable(isThreadable),
75        mBuildChecksum(buildChecksum), mRS(RSContext) {
76    }
77
78    ~ScriptExecutable() {
79        for (size_t i = 0; i < mExportedVarCount; ++i) {
80            if (mFieldIsObject[i]) {
81                if (mFieldAddress[i] != nullptr) {
82                    rs_object_base *obj_addr =
83                            reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
84                    rsrClearObject(mRS, obj_addr);
85                }
86            }
87        }
88
89        delete[] mBuildChecksum;
90        for (size_t i = 0; i < mPragmaCount; ++i) {
91            delete [] mPragmaKeys[i];
92            delete [] mPragmaValues[i];
93        }
94        delete[] mPragmaValues;
95        delete[] mPragmaKeys;
96
97        delete[] mForEachSignatures;
98        delete[] mForEachFunctions;
99
100        delete[] mInvokeFunctions;
101
102        for (size_t i = 0; i < mExportedVarCount; i++) {
103            delete[] mFieldName[i];
104        }
105        delete[] mFieldName;
106        delete[] mFieldIsObject;
107        delete[] mFieldAddress;
108    }
109
110    static ScriptExecutable*
111            createFromSharedObject(Context* RSContext, void* sharedObj);
112
113    size_t getExportedVariableCount() const { return mExportedVarCount; }
114    size_t getExportedFunctionCount() const { return mFuncCount; }
115    size_t getExportedForEachCount() const { return mForEachCount; }
116    size_t getPragmaCount() const { return mPragmaCount; }
117
118    void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
119    void* getFieldAddress(const char* name) const;
120    bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
121    const char* getFieldName(int slot) const { return mFieldName[slot]; }
122
123    InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
124
125    ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
126    uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
127
128    const char ** getPragmaKeys() const { return mPragmaKeys; }
129    const char ** getPragmaValues() const { return mPragmaValues; }
130
131    bool getThreadable() const { return mIsThreadable; }
132
133    const char *getBuildChecksum() const { return mBuildChecksum; }
134    bool isChecksumValid(const char *checksum) const {
135      return (mBuildChecksum != nullptr &&
136              strcmp(checksum, mBuildChecksum) == 0);
137    }
138
139private:
140    void** mFieldAddress;
141    bool* mFieldIsObject;
142    const char* const * mFieldName;
143    size_t mExportedVarCount;
144
145    InvokeFunc_t* mInvokeFunctions;
146    size_t mFuncCount;
147
148    ForEachFunc_t* mForEachFunctions;
149    uint32_t* mForEachSignatures;
150    size_t mForEachCount;
151
152    const char ** mPragmaKeys;
153    const char ** mPragmaValues;
154    size_t mPragmaCount;
155
156    bool mIsThreadable;
157    const char *mBuildChecksum;
158
159    Context* mRS;
160};
161
162}  // namespace renderscript
163}  // namespace android
164
165#endif  // ANDROID_RENDERSCRIPT_EXECUTABLE_H
166