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
28namespace bcc {
29    class BCCContext;
30    class RSCompilerDriver;
31    class RSExecutable;
32}
33
34namespace android {
35namespace renderscript {
36
37typedef struct {
38  uint32_t eStride;
39  uint32_t yStride;
40} StridePair;
41
42typedef struct {
43    const void *in;
44    void *out;
45    const void *usr;
46    uint32_t usrLen;
47    uint32_t x;
48    uint32_t y;
49    uint32_t z;
50    uint32_t lod;
51    RsAllocationCubemapFace face;
52    uint32_t ar[16];
53
54    const void **ins;
55    uint32_t *eStrideIns;
56
57    uint32_t lid;
58
59    uint32_t dimX;
60    uint32_t dimY;
61    uint32_t dimZ;
62    uint32_t dimArray;
63
64    const uint8_t *ptrIn;
65    uint8_t *ptrOut;
66    uint32_t eStrideIn;
67    uint32_t eStrideOut;
68    uint32_t yStrideIn;
69    uint32_t yStrideOut;
70    uint32_t slot;
71
72    const uint8_t** ptrIns;
73    StridePair* inStrides;
74} RsForEachStubParamStruct;
75
76extern bool gArchUseSIMD;
77
78typedef void (* InvokeFunc_t)(void);
79typedef void (* ForEachFunc_t)(void);
80typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
81
82class RsdCpuScriptImpl;
83class RsdCpuReferenceImpl;
84
85typedef struct ScriptTLSStructRec {
86    android::renderscript::Context * mContext;
87    const android::renderscript::Script * mScript;
88    RsdCpuScriptImpl *mImpl;
89} ScriptTLSStruct;
90
91typedef struct {
92    RsForEachStubParamStruct fep;
93
94    RsdCpuReferenceImpl *rsc;
95    RsdCpuScriptImpl *script;
96
97    ForEachFunc_t kernel;
98    uint32_t sig;
99    const Allocation * ain;
100    Allocation * aout;
101
102    uint32_t mSliceSize;
103    volatile int mSliceNum;
104    bool isThreadable;
105
106    uint32_t xStart;
107    uint32_t xEnd;
108    uint32_t yStart;
109    uint32_t yEnd;
110    uint32_t zStart;
111    uint32_t zEnd;
112    uint32_t arrayStart;
113    uint32_t arrayEnd;
114
115    // Multi-input data.
116    const Allocation ** ains;
117} MTLaunchStruct;
118
119
120
121
122class RsdCpuReferenceImpl : public RsdCpuReference {
123public:
124    virtual ~RsdCpuReferenceImpl();
125    RsdCpuReferenceImpl(Context *);
126
127    void lockMutex();
128    void unlockMutex();
129
130    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
131    virtual void setPriority(int32_t priority);
132    virtual void launchThreads(WorkerCallback_t cbk, void *data);
133    static void * helperThreadProc(void *vrsc);
134    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
135
136    Context * getContext() {return mRSC;}
137    uint32_t getThreadCount() const {
138        return mWorkers.mCount + 1;
139    }
140
141    void launchThreads(const Allocation * ain, Allocation * aout,
142                       const RsScriptCall *sc, MTLaunchStruct *mtls);
143
144    void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
145                       const RsScriptCall* sc, MTLaunchStruct* mtls);
146
147    virtual CpuScript * createScript(const ScriptC *s,
148                                     char const *resName, char const *cacheDir,
149                                     uint8_t const *bitcode, size_t bitcodeSize,
150                                     uint32_t flags);
151    virtual CpuScript * createIntrinsic(const Script *s,
152                                        RsScriptIntrinsicID iid, Element *e);
153    virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg);
154
155    const RsdCpuReference::CpuSymbol *symLookup(const char *);
156
157    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
158        return mScriptLookupFn(mRSC, s);
159    }
160
161#ifndef RS_COMPATIBILITY_LIB
162    void setLinkRuntimeCallback(
163            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
164        mLinkRuntimeCallback = pLinkRuntimeCallback;
165    }
166    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
167        return mLinkRuntimeCallback;
168    }
169
170    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
171        mSelectRTCallback = pSelectRTCallback;
172    }
173    RSSelectRTCallback getSelectRTCallback() {
174        return mSelectRTCallback;
175    }
176
177    virtual void setSetupCompilerCallback(
178            RSSetupCompilerCallback pSetupCompilerCallback) {
179        mSetupCompilerCallback = pSetupCompilerCallback;
180    }
181    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
182        return mSetupCompilerCallback;
183    }
184
185    virtual void setBccPluginName(const char *name) {
186        mBccPluginName.assign(name);
187    }
188    virtual const char *getBccPluginName() const {
189        return mBccPluginName.c_str();
190    }
191#endif
192    virtual bool getInForEach() { return mInForEach; }
193
194protected:
195    Context *mRSC;
196    uint32_t version_major;
197    uint32_t version_minor;
198    //bool mHasGraphics;
199    bool mInForEach;
200
201    struct Workers {
202        volatile int mRunningCount;
203        volatile int mLaunchCount;
204        uint32_t mCount;
205        pthread_t *mThreadId;
206        pid_t *mNativeThreadId;
207        Signal mCompleteSignal;
208        Signal *mLaunchSignals;
209        WorkerCallback_t mLaunchCallback;
210        void *mLaunchData;
211    };
212    Workers mWorkers;
213    bool mExit;
214    sym_lookup_t mSymLookupFn;
215    script_lookup_t mScriptLookupFn;
216
217    ScriptTLSStruct mTlsStruct;
218
219#ifndef RS_COMPATIBILITY_LIB
220    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
221    RSSelectRTCallback mSelectRTCallback;
222    RSSetupCompilerCallback mSetupCompilerCallback;
223    std::string mBccPluginName;
224#endif
225};
226
227
228}
229}
230
231#endif
232