rsCpuExecutable.h revision 2abfcc6d129fe3defddef4540aa95cc445c03a7a
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) :
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), mRS(RSContext) {
75    }
76
77    ~ScriptExecutable() {
78        for (size_t i = 0; i < mExportedVarCount; ++i) {
79            if (mFieldIsObject[i]) {
80                if (mFieldAddress[i] != nullptr) {
81                    rs_object_base *obj_addr =
82                            reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
83                    rsrClearObject(mRS, obj_addr);
84                }
85            }
86        }
87
88        for (size_t i = 0; i < mPragmaCount; ++i) {
89            delete [] mPragmaKeys[i];
90            delete [] mPragmaValues[i];
91        }
92
93        delete[] mPragmaValues;
94        delete[] mPragmaKeys;
95        delete[] mForEachSignatures;
96        delete[] mForEachFunctions;
97        delete[] mInvokeFunctions;
98        delete[] mFieldIsObject;
99        delete[] mFieldAddress;
100    }
101
102    static ScriptExecutable*
103            createFromSharedObject(Context* RSContext, void* sharedObj);
104
105    size_t getExportedVariableCount() const { return mExportedVarCount; }
106    size_t getExportedFunctionCount() const { return mFuncCount; }
107    size_t getExportedForEachCount() const { return mForEachCount; }
108    size_t getPragmaCount() const { return mPragmaCount; }
109
110    void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
111    bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
112    InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
113    ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
114    uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
115
116    const char ** getPragmaKeys() const { return mPragmaKeys; }
117    const char ** getPragmaValues() const { return mPragmaValues; }
118
119    bool getThreadable() const { return mIsThreadable; }
120
121private:
122    void** mFieldAddress;
123    bool* mFieldIsObject;
124    size_t mExportedVarCount;
125
126    InvokeFunc_t* mInvokeFunctions;
127    size_t mFuncCount;
128
129    ForEachFunc_t* mForEachFunctions;
130    uint32_t* mForEachSignatures;
131    size_t mForEachCount;
132
133    const char ** mPragmaKeys;
134    const char ** mPragmaValues;
135    size_t mPragmaCount;
136
137    bool mIsThreadable;
138
139    Context* mRS;
140};
141
142}  // namespace renderscript
143}  // namespace android
144
145#endif  // ANDROID_RENDERSCRIPT_EXECUTABLE_H
146