rsCpuCore.h revision dced5c96bc53c45a1aac782ea9bd738b0d50fd09
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 "rsCppUtils.h"
24#include "rsElement.h"
25#include "rsScriptC.h"
26#include "rsCpuCoreRuntime.h"
27
28
29#define RS_KERNEL_INPUT_LIMIT 8
30
31namespace bcc {
32    class BCCContext;
33    class RSCompilerDriver;
34    class RSExecutable;
35}
36
37namespace android {
38namespace renderscript {
39
40struct StridePair {
41  uint32_t eStride;
42  uint32_t yStride;
43};
44
45struct RsLaunchDimensions {
46    uint32_t x;
47    uint32_t y;
48    uint32_t z;
49    uint32_t lod;
50    uint32_t face;
51    uint32_t array[4 /*make a define*/];
52};
53
54struct RsExpandKernelDriverInfo {
55    // Warning: This structure is shared with the compiler
56    // Any change to the fields here requires a matching compiler change
57
58    const uint8_t *inPtr[RS_KERNEL_INPUT_LIMIT];
59    uint32_t inStride[RS_KERNEL_INPUT_LIMIT];
60    uint32_t inLen;
61
62    uint8_t *outPtr[RS_KERNEL_INPUT_LIMIT];
63    uint32_t outStride[RS_KERNEL_INPUT_LIMIT];
64    uint32_t outLen;
65
66    // Dimension of the launch
67    RsLaunchDimensions dim;
68
69    // The walking itterator of the launch
70    RsLaunchDimensions current;
71
72    const void *usr;
73    uint32_t usrLen;
74
75
76
77    // Items below this line are not used by the compiler and can be change in the driver
78    uint32_t lid;
79    uint32_t slot;
80
81};
82
83typedef ::RsExpandKernelParams RsExpandKernelParams;
84
85extern bool gArchUseSIMD;
86
87typedef void (* InvokeFunc_t)(void);
88typedef void (* ForEachFunc_t)(void);
89typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
90
91class RsdCpuScriptImpl;
92class RsdCpuReferenceImpl;
93
94struct ScriptTLSStruct {
95    android::renderscript::Context * mContext;
96    const android::renderscript::Script * mScript;
97    RsdCpuScriptImpl *mImpl;
98};
99
100struct MTLaunchStruct {
101    RsExpandKernelDriverInfo fep;
102
103    RsdCpuReferenceImpl *rsc;
104    RsdCpuScriptImpl *script;
105
106    ForEachFunc_t kernel;
107    uint32_t sig;
108    const Allocation * ains[RS_KERNEL_INPUT_LIMIT];
109    Allocation * aout[RS_KERNEL_INPUT_LIMIT];
110
111    uint32_t mSliceSize;
112    volatile int mSliceNum;
113    bool isThreadable;
114
115    RsLaunchDimensions start;
116    RsLaunchDimensions end;
117};
118
119class RsdCpuReferenceImpl : public RsdCpuReference {
120public:
121    virtual ~RsdCpuReferenceImpl();
122    RsdCpuReferenceImpl(Context *);
123
124    void lockMutex();
125    void unlockMutex();
126
127    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
128    virtual void setPriority(int32_t priority);
129    virtual void launchThreads(WorkerCallback_t cbk, void *data);
130    static void * helperThreadProc(void *vrsc);
131    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
132
133    Context * getContext() {return mRSC;}
134    uint32_t getThreadCount() const {
135        return mWorkers.mCount + 1;
136    }
137
138    void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
139                       const RsScriptCall* sc, MTLaunchStruct* mtls);
140
141    virtual CpuScript * createScript(const ScriptC *s,
142                                     char const *resName, char const *cacheDir,
143                                     uint8_t const *bitcode, size_t bitcodeSize,
144                                     uint32_t flags);
145    virtual CpuScript * createIntrinsic(const Script *s,
146                                        RsScriptIntrinsicID iid, Element *e);
147    virtual void* createScriptGroup(const ScriptGroupBase *sg);
148
149    const RsdCpuReference::CpuSymbol *symLookup(const char *);
150
151    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
152        return mScriptLookupFn(mRSC, s);
153    }
154
155    void setLinkRuntimeCallback(
156            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
157        mLinkRuntimeCallback = pLinkRuntimeCallback;
158    }
159    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
160        return mLinkRuntimeCallback;
161    }
162
163    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
164        mSelectRTCallback = pSelectRTCallback;
165    }
166    RSSelectRTCallback getSelectRTCallback() {
167        return mSelectRTCallback;
168    }
169
170    virtual void setSetupCompilerCallback(
171            RSSetupCompilerCallback pSetupCompilerCallback) {
172        mSetupCompilerCallback = pSetupCompilerCallback;
173    }
174    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
175        return mSetupCompilerCallback;
176    }
177
178    virtual void setBccPluginName(const char *name) {
179        mBccPluginName.setTo(name);
180    }
181    virtual const char *getBccPluginName() const {
182        return mBccPluginName.string();
183    }
184    virtual bool getInForEach() { return mInForEach; }
185
186protected:
187    Context *mRSC;
188    uint32_t version_major;
189    uint32_t version_minor;
190    //bool mHasGraphics;
191    bool mInForEach;
192
193    struct Workers {
194        volatile int mRunningCount;
195        volatile int mLaunchCount;
196        uint32_t mCount;
197        pthread_t *mThreadId;
198        pid_t *mNativeThreadId;
199        Signal mCompleteSignal;
200        Signal *mLaunchSignals;
201        WorkerCallback_t mLaunchCallback;
202        void *mLaunchData;
203    };
204    Workers mWorkers;
205    bool mExit;
206    sym_lookup_t mSymLookupFn;
207    script_lookup_t mScriptLookupFn;
208
209    ScriptTLSStruct mTlsStruct;
210
211    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
212    RSSelectRTCallback mSelectRTCallback;
213    RSSetupCompilerCallback mSetupCompilerCallback;
214    String8 mBccPluginName;
215};
216
217
218}
219}
220
221#endif
222