rsCpuCore.h revision 44bef6fba6244292b751387f3d6c31cca96c28ad
1/*
2 * Copyright (C) 2012 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 RSD_CPU_CORE_H
18#define RSD_CPU_CORE_H
19
20#include "rsd_cpu.h"
21#include "rsSignal.h"
22#include "rsContext.h"
23#include "rsElement.h"
24#include "rsScriptC.h"
25
26#include <string>
27
28#define RS_KERNEL_INPUT_THRESHOLD 32
29
30namespace bcc {
31    class BCCContext;
32    class RSCompilerDriver;
33    class RSExecutable;
34}
35
36namespace android {
37namespace renderscript {
38
39struct StridePair {
40  uint32_t eStride;
41  uint32_t yStride;
42};
43
44struct RsExpandKernelDriverInfo {
45    const uint8_t **inPtrs;
46    uint32_t inLen;
47
48    uint8_t *outPtr;
49
50    StridePair *inStrides;
51    StridePair  outStride;
52
53    uint32_t dimX;
54    uint32_t dimY;
55    uint32_t dimZ;
56
57    uint32_t slot;
58
59    const void *usr;
60    uint32_t usrLen;
61
62    bool heapAllocatedArrays;
63
64    RsExpandKernelDriverInfo() : heapAllocatedArrays(false) {}
65
66    ~RsExpandKernelDriverInfo() {
67        if (heapAllocatedArrays) {
68            if (inPtrs != nullptr) {
69                delete[] inPtrs;
70            }
71
72            if (inStrides != nullptr) {
73                delete[] inStrides;
74            }
75        }
76    }
77};
78
79struct RsExpandKernelParams {
80
81    // Used by kernels
82    const void **ins;
83    uint32_t *inEStrides;
84    void *out;
85    uint32_t y;
86    uint32_t z;
87    uint32_t lid;
88
89    // Used by ScriptGroup and user kernels.
90    const void *usr;
91
92    // Used by intrinsics
93    uint32_t dimX;
94    uint32_t dimY;
95    uint32_t dimZ;
96
97    /*
98     * FIXME: This is only used by the blend intrinsic.  If possible, we should
99     *        modify blur to not need it.
100     */
101    uint32_t slot;
102
103    /// Copy fields needed by a kernel from a driver struct.
104    void takeFields(const RsExpandKernelDriverInfo &dstruct) {
105        this->usr  = dstruct.usr;
106        this->slot = dstruct.slot;
107
108        this->dimX = dstruct.dimX;
109        this->dimY = dstruct.dimY;
110        this->dimZ = dstruct.dimZ;
111    }
112};
113
114extern bool gArchUseSIMD;
115
116typedef void (* InvokeFunc_t)(void);
117typedef void (* ForEachFunc_t)(void);
118typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
119
120class RsdCpuScriptImpl;
121class RsdCpuReferenceImpl;
122
123struct ScriptTLSStruct {
124    android::renderscript::Context * mContext;
125    const android::renderscript::Script * mScript;
126    RsdCpuScriptImpl *mImpl;
127};
128
129struct MTLaunchStruct {
130    RsExpandKernelDriverInfo fep;
131
132    RsdCpuReferenceImpl *rsc;
133    RsdCpuScriptImpl *script;
134
135    ForEachFunc_t kernel;
136    uint32_t sig;
137    const Allocation ** ains;
138    Allocation * aout;
139
140    uint32_t mSliceSize;
141    volatile int mSliceNum;
142    bool isThreadable;
143
144    uint32_t xStart;
145    uint32_t xEnd;
146    uint32_t yStart;
147    uint32_t yEnd;
148    uint32_t zStart;
149    uint32_t zEnd;
150    uint32_t arrayStart;
151    uint32_t arrayEnd;
152
153    const uint8_t *inPtrsBuff[RS_KERNEL_INPUT_THRESHOLD];
154    StridePair     inStridesBuff[RS_KERNEL_INPUT_THRESHOLD];
155};
156
157class RsdCpuReferenceImpl : public RsdCpuReference {
158public:
159    virtual ~RsdCpuReferenceImpl();
160    RsdCpuReferenceImpl(Context *);
161
162    void lockMutex();
163    void unlockMutex();
164
165    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
166    virtual void setPriority(int32_t priority);
167    virtual void launchThreads(WorkerCallback_t cbk, void *data);
168    static void * helperThreadProc(void *vrsc);
169    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
170
171    Context * getContext() {return mRSC;}
172    uint32_t getThreadCount() const {
173        return mWorkers.mCount + 1;
174    }
175
176    void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
177                       const RsScriptCall* sc, MTLaunchStruct* mtls);
178
179    virtual CpuScript * createScript(const ScriptC *s,
180                                     char const *resName, char const *cacheDir,
181                                     uint8_t const *bitcode, size_t bitcodeSize,
182                                     uint32_t flags);
183    virtual CpuScript * createIntrinsic(const Script *s,
184                                        RsScriptIntrinsicID iid, Element *e);
185    virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg);
186
187    const RsdCpuReference::CpuSymbol *symLookup(const char *);
188
189    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
190        return mScriptLookupFn(mRSC, s);
191    }
192
193#ifndef RS_COMPATIBILITY_LIB
194    void setLinkRuntimeCallback(
195            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
196        mLinkRuntimeCallback = pLinkRuntimeCallback;
197    }
198    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
199        return mLinkRuntimeCallback;
200    }
201
202    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
203        mSelectRTCallback = pSelectRTCallback;
204    }
205    RSSelectRTCallback getSelectRTCallback() {
206        return mSelectRTCallback;
207    }
208
209    virtual void setSetupCompilerCallback(
210            RSSetupCompilerCallback pSetupCompilerCallback) {
211        mSetupCompilerCallback = pSetupCompilerCallback;
212    }
213    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
214        return mSetupCompilerCallback;
215    }
216
217    virtual void setBccPluginName(const char *name) {
218        mBccPluginName.assign(name);
219    }
220    virtual const char *getBccPluginName() const {
221        return mBccPluginName.c_str();
222    }
223#endif
224    virtual bool getInForEach() { return mInForEach; }
225
226protected:
227    Context *mRSC;
228    uint32_t version_major;
229    uint32_t version_minor;
230    //bool mHasGraphics;
231    bool mInForEach;
232
233    struct Workers {
234        volatile int mRunningCount;
235        volatile int mLaunchCount;
236        uint32_t mCount;
237        pthread_t *mThreadId;
238        pid_t *mNativeThreadId;
239        Signal mCompleteSignal;
240        Signal *mLaunchSignals;
241        WorkerCallback_t mLaunchCallback;
242        void *mLaunchData;
243    };
244    Workers mWorkers;
245    bool mExit;
246    sym_lookup_t mSymLookupFn;
247    script_lookup_t mScriptLookupFn;
248
249    ScriptTLSStruct mTlsStruct;
250
251#ifndef RS_COMPATIBILITY_LIB
252    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
253    RSSelectRTCallback mSelectRTCallback;
254    RSSetupCompilerCallback mSetupCompilerCallback;
255    std::string mBccPluginName;
256#endif
257};
258
259
260}
261}
262
263#endif
264