rsCpuExecutable.h revision aa6757ffc1b23d771566439c3179fdbc1e5ba569
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, size_t varCount,
61                     InvokeFunc_t* invokeFunctions, size_t funcCount,
62                     ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures,
63                     size_t forEachCount,
64                     const char ** pragmaKeys, const char ** pragmaValues,
65                     size_t pragmaCount,
66                     bool isThreadable, const char *buildChecksum) :
67        mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
68            mExportedVarCount(varCount),
69            mInvokeFunctions(invokeFunctions), mFuncCount(funcCount),
70            mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
71            mForEachCount(forEachCount),
72            mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
73            mPragmaCount(pragmaCount),
74            mIsThreadable(isThreadable), mBuildChecksum(buildChecksum),
75            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
95        delete[] mPragmaValues;
96        delete[] mPragmaKeys;
97        delete[] mForEachSignatures;
98        delete[] mForEachFunctions;
99        delete[] mInvokeFunctions;
100        delete[] mFieldIsObject;
101        delete[] mFieldAddress;
102    }
103
104    static ScriptExecutable*
105            createFromSharedObject(Context* RSContext, void* sharedObj);
106
107    size_t getExportedVariableCount() const { return mExportedVarCount; }
108    size_t getExportedFunctionCount() const { return mFuncCount; }
109    size_t getExportedForEachCount() const { return mForEachCount; }
110    size_t getPragmaCount() const { return mPragmaCount; }
111
112    void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
113    bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
114    InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
115    ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
116    uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
117
118    const char ** getPragmaKeys() const { return mPragmaKeys; }
119    const char ** getPragmaValues() const { return mPragmaValues; }
120
121    bool getThreadable() const { return mIsThreadable; }
122
123    const char *getBuildChecksum() const { return mBuildChecksum; }
124    bool isChecksumValid(const char *checksum) const {
125      return (mBuildChecksum != nullptr &&
126              strcmp(checksum, mBuildChecksum) == 0);
127    }
128
129private:
130    void** mFieldAddress;
131    bool* mFieldIsObject;
132    size_t mExportedVarCount;
133
134    InvokeFunc_t* mInvokeFunctions;
135    size_t mFuncCount;
136
137    ForEachFunc_t* mForEachFunctions;
138    uint32_t* mForEachSignatures;
139    size_t mForEachCount;
140
141    const char ** mPragmaKeys;
142    const char ** mPragmaValues;
143    size_t mPragmaCount;
144
145    bool mIsThreadable;
146    const char *mBuildChecksum;
147
148    Context* mRS;
149};
150
151}  // namespace renderscript
152}  // namespace android
153
154#endif  // ANDROID_RENDERSCRIPT_EXECUTABLE_H
155