rsCpuCore.h revision c0d68470b978a79ce024fde56f23ea3690603ccd
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_LIMIT 8
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 RsLaunchDimensions {
45    uint32_t x;
46    uint32_t y;
47    uint32_t z;
48    uint32_t lod;
49    uint32_t faces;
50    uint32_t array[4 /*make a define*/];
51};
52
53struct RsExpandKernelDriverInfo {
54    // Warning: This structure is shared with the compiler
55    // Any change to the fields here requires a matching compiler change
56
57    const uint8_t *inPtr[RS_KERNEL_INPUT_LIMIT];
58    uint32_t inStride[RS_KERNEL_INPUT_LIMIT];
59    uint32_t inLen;
60
61    uint8_t *outPtr[RS_KERNEL_INPUT_LIMIT];
62    uint32_t outStride[RS_KERNEL_INPUT_LIMIT];
63    uint32_t outLen;
64
65    // Dimension of the launch
66    RsLaunchDimensions dim;
67
68    // The walking itterator of the launch
69    RsLaunchDimensions current;
70
71    const void *usr;
72    uint32_t usrLen;
73
74
75
76    // Items below this line are not used by the compiler and can be change in the driver
77    uint32_t lid;
78    uint32_t slot;
79
80};
81
82struct RsExpandKernelParams {
83
84    // Used by kernels
85    const void **ins;
86    uint32_t *inEStrides;
87    void *out;
88    uint32_t y;
89    uint32_t z;
90    uint32_t lid;
91
92    // Used by ScriptGroup and user kernels.
93    const void *usr;
94
95    // Used by intrinsics
96    uint32_t dimX;
97    uint32_t dimY;
98    uint32_t dimZ;
99
100    /*
101     * FIXME: This is only used by the blend intrinsic.  If possible, we should
102     *        modify blur to not need it.
103     */
104    uint32_t slot;
105};
106
107extern bool gArchUseSIMD;
108
109typedef void (* InvokeFunc_t)(void);
110typedef void (* ForEachFunc_t)(void);
111typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
112
113class RsdCpuScriptImpl;
114class RsdCpuReferenceImpl;
115
116struct ScriptTLSStruct {
117    android::renderscript::Context * mContext;
118    const android::renderscript::Script * mScript;
119    RsdCpuScriptImpl *mImpl;
120};
121
122struct MTLaunchStruct {
123    RsExpandKernelDriverInfo fep;
124
125    RsdCpuReferenceImpl *rsc;
126    RsdCpuScriptImpl *script;
127
128    ForEachFunc_t kernel;
129    uint32_t sig;
130    const Allocation * ains[RS_KERNEL_INPUT_LIMIT];
131    Allocation * aout[RS_KERNEL_INPUT_LIMIT];
132
133    uint32_t mSliceSize;
134    volatile int mSliceNum;
135    bool isThreadable;
136
137    // origin of the launch
138    RsLaunchDimensions origin;
139
140    // TODO: convert to RsLaunchDimensions
141    uint32_t xStart;
142    uint32_t xEnd;
143    uint32_t yStart;
144    uint32_t yEnd;
145    uint32_t zStart;
146    uint32_t zEnd;
147    uint32_t arrayStart;
148    uint32_t arrayEnd;
149};
150
151class RsdCpuReferenceImpl : public RsdCpuReference {
152public:
153    virtual ~RsdCpuReferenceImpl();
154    RsdCpuReferenceImpl(Context *);
155
156    void lockMutex();
157    void unlockMutex();
158
159    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
160    virtual void setPriority(int32_t priority);
161    virtual void launchThreads(WorkerCallback_t cbk, void *data);
162    static void * helperThreadProc(void *vrsc);
163    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
164
165    Context * getContext() {return mRSC;}
166    uint32_t getThreadCount() const {
167        return mWorkers.mCount + 1;
168    }
169
170    void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
171                       const RsScriptCall* sc, MTLaunchStruct* mtls);
172
173    virtual CpuScript * createScript(const ScriptC *s,
174                                     char const *resName, char const *cacheDir,
175                                     uint8_t const *bitcode, size_t bitcodeSize,
176                                     uint32_t flags);
177    virtual CpuScript * createIntrinsic(const Script *s,
178                                        RsScriptIntrinsicID iid, Element *e);
179    virtual void* createScriptGroup(const ScriptGroupBase *sg);
180
181    const RsdCpuReference::CpuSymbol *symLookup(const char *);
182
183    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
184        return mScriptLookupFn(mRSC, s);
185    }
186
187    void setLinkRuntimeCallback(
188            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
189        mLinkRuntimeCallback = pLinkRuntimeCallback;
190    }
191    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
192        return mLinkRuntimeCallback;
193    }
194
195    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
196        mSelectRTCallback = pSelectRTCallback;
197    }
198    RSSelectRTCallback getSelectRTCallback() {
199        return mSelectRTCallback;
200    }
201
202    virtual void setSetupCompilerCallback(
203            RSSetupCompilerCallback pSetupCompilerCallback) {
204        mSetupCompilerCallback = pSetupCompilerCallback;
205    }
206    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
207        return mSetupCompilerCallback;
208    }
209
210    virtual void setBccPluginName(const char *name) {
211        mBccPluginName.assign(name);
212    }
213    virtual const char *getBccPluginName() const {
214        return mBccPluginName.c_str();
215    }
216    virtual bool getInForEach() { return mInForEach; }
217
218protected:
219    Context *mRSC;
220    uint32_t version_major;
221    uint32_t version_minor;
222    //bool mHasGraphics;
223    bool mInForEach;
224
225    struct Workers {
226        volatile int mRunningCount;
227        volatile int mLaunchCount;
228        uint32_t mCount;
229        pthread_t *mThreadId;
230        pid_t *mNativeThreadId;
231        Signal mCompleteSignal;
232        Signal *mLaunchSignals;
233        WorkerCallback_t mLaunchCallback;
234        void *mLaunchData;
235    };
236    Workers mWorkers;
237    bool mExit;
238    sym_lookup_t mSymLookupFn;
239    script_lookup_t mScriptLookupFn;
240
241    ScriptTLSStruct mTlsStruct;
242
243    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
244    RSSelectRTCallback mSelectRTCallback;
245    RSSetupCompilerCallback mSetupCompilerCallback;
246    std::string mBccPluginName;
247};
248
249
250}
251}
252
253#endif
254