rsCpuCore.h revision 6c1876bbef1b2c89975dce91230a168bd2d2ce4c
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
28namespace android {
29namespace renderscript {
30
31// Whether the CPU we're running on supports SIMD instructions
32extern bool gArchUseSIMD;
33
34// Function types found in RenderScript code
35typedef void (*ReduceFunc_t)(const uint8_t *inBuf, uint8_t *outBuf, uint32_t len);
36typedef void (*ReduceNewAccumulatorFunc_t)(const RsExpandKernelDriverInfo *info, uint32_t x1, uint32_t x2, uint8_t *accum);
37typedef void (*ReduceNewInitializerFunc_t)(uint8_t *accum);
38typedef void (*ReduceNewOutConverterFunc_t)(uint8_t *out, const uint8_t *accum);
39typedef void (*ForEachFunc_t)(const RsExpandKernelDriverInfo *info, uint32_t x1, uint32_t x2, uint32_t outStride);
40typedef void (*InvokeFunc_t)(void *params);
41typedef void (*InitOrDtorFunc_t)(void);
42typedef int  (*RootFunc_t)(void);
43
44struct ReduceNewDescription {
45    ReduceNewAccumulatorFunc_t  accumFunc;  // expanded accumulator function
46    ReduceNewInitializerFunc_t  initFunc;   // user initializer function
47    ReduceNewOutConverterFunc_t outFunc;    // user outconverter function
48    size_t                      accumSize;  // accumulator datum size, in bytes
49};
50
51// Internal driver callback used to execute a kernel
52typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
53
54class RsdCpuScriptImpl;
55class RsdCpuReferenceImpl;
56
57struct ScriptTLSStruct {
58    android::renderscript::Context * mContext;
59    const android::renderscript::Script * mScript;
60    RsdCpuScriptImpl *mImpl;
61};
62
63// MTLaunchStruct passes information about a multithreaded kernel launch.
64struct MTLaunchStructCommon {
65    RsdCpuReferenceImpl *rs;
66    RsdCpuScriptImpl *script;
67
68    uint32_t mSliceSize;
69    volatile int mSliceNum;
70    bool isThreadable;
71
72    // Boundary information about the launch
73    RsLaunchDimensions start;
74    RsLaunchDimensions end;
75    // Points to MTLaunchStructForEach::fep::dim or
76    // MTLaunchStructReduce::inputDim.
77    RsLaunchDimensions *dimPtr;
78};
79
80struct MTLaunchStructForEach : public MTLaunchStructCommon {
81    // Driver info structure
82    RsExpandKernelDriverInfo fep;
83
84    ForEachFunc_t kernel;
85    const Allocation *ains[RS_KERNEL_INPUT_LIMIT];
86    Allocation *aout[RS_KERNEL_INPUT_LIMIT];
87};
88
89struct MTLaunchStructReduce : public MTLaunchStructCommon {
90    ReduceFunc_t kernel;
91    const uint8_t *inBuf;
92    uint8_t *outBuf;
93    RsLaunchDimensions inputDim;
94};
95
96struct MTLaunchStructReduceNew : public MTLaunchStructCommon {
97    // Driver info structure
98    RsExpandKernelDriverInfo redp;
99
100    const Allocation *ains[RS_KERNEL_INPUT_LIMIT];
101
102    ReduceNewAccumulatorFunc_t accumFunc;
103    ReduceNewInitializerFunc_t initFunc;
104    ReduceNewOutConverterFunc_t outFunc;
105
106    size_t accumSize;  // accumulator datum size in bytes
107};
108
109class RsdCpuReferenceImpl : public RsdCpuReference {
110public:
111    ~RsdCpuReferenceImpl() override;
112    RsdCpuReferenceImpl(Context *);
113
114    void lockMutex();
115    void unlockMutex();
116
117    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
118    void setPriority(int32_t priority) override;
119    virtual void launchThreads(WorkerCallback_t cbk, void *data);
120    static void * helperThreadProc(void *vrsc);
121    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
122
123    Context * getContext() {return mRSC;}
124    uint32_t getThreadCount() const {
125        return mWorkers.mCount + 1;
126    }
127
128    // Launch foreach kernel
129    void launchForEach(const Allocation **ains, uint32_t inLen, Allocation *aout,
130                       const RsScriptCall *sc, MTLaunchStructForEach *mtls);
131
132    // Launch a simple reduce kernel
133    void launchReduce(const Allocation *ain, Allocation *aout,
134                      MTLaunchStructReduce *mtls);
135
136    // Launch a general reduce kernel
137    void launchReduceNew(const Allocation ** ains, uint32_t inLen, Allocation *aout,
138                         MTLaunchStructReduceNew *mtls);
139
140    CpuScript * createScript(const ScriptC *s, char const *resName, char const *cacheDir,
141                             uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags) override;
142    CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) override;
143    void* createScriptGroup(const ScriptGroupBase *sg) override;
144
145    const RsdCpuReference::CpuSymbol *symLookup(const char *);
146
147    RsdCpuReference::CpuScript *lookupScript(const Script *s) {
148        return mScriptLookupFn(mRSC, s);
149    }
150
151    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
152        mSelectRTCallback = pSelectRTCallback;
153    }
154    RSSelectRTCallback getSelectRTCallback() {
155        return mSelectRTCallback;
156    }
157
158    virtual void setBccPluginName(const char *name) {
159        mBccPluginName.setTo(name);
160    }
161    virtual const char *getBccPluginName() const {
162        return mBccPluginName.string();
163    }
164    bool getInForEach() override { return mInForEach; }
165
166    // Set to true if we should embed global variable information in the code.
167    void setEmbedGlobalInfo(bool v) override {
168        mEmbedGlobalInfo = v;
169    }
170
171    // Returns true if we should embed global variable information in the code.
172    bool getEmbedGlobalInfo() const override {
173        return mEmbedGlobalInfo;
174    }
175
176    // Set to true if we should skip constant (immutable) global variables when
177    // potentially embedding information about globals.
178    void setEmbedGlobalInfoSkipConstant(bool v) override {
179        mEmbedGlobalInfoSkipConstant = v;
180    }
181
182    // Returns true if we should skip constant (immutable) global variables when
183    // potentially embedding information about globals.
184    bool getEmbedGlobalInfoSkipConstant() const override {
185        return mEmbedGlobalInfoSkipConstant;
186    }
187
188protected:
189    Context *mRSC;
190    uint32_t version_major;
191    uint32_t version_minor;
192    //bool mHasGraphics;
193    bool mInForEach;
194
195    struct Workers {
196        volatile int mRunningCount;
197        volatile int mLaunchCount;
198        uint32_t mCount;
199        pthread_t *mThreadId;
200        pid_t *mNativeThreadId;
201        Signal mCompleteSignal;
202        Signal *mLaunchSignals;
203        WorkerCallback_t mLaunchCallback;
204        void *mLaunchData;
205    };
206    Workers mWorkers;
207    bool mExit;
208    sym_lookup_t mSymLookupFn;
209    script_lookup_t mScriptLookupFn;
210
211    ScriptTLSStruct mTlsStruct;
212
213    RSSelectRTCallback mSelectRTCallback;
214    String8 mBccPluginName;
215
216    // Specifies whether we should embed global variable information in the
217    // code via special RS variables that can be examined later by the driver.
218    // Defaults to true.
219    bool mEmbedGlobalInfo;
220
221    // Specifies whether we should skip constant (immutable) global variables
222    // when potentially embedding information about globals.
223    // Defaults to true.
224    bool mEmbedGlobalInfoSkipConstant;
225};
226
227
228}
229}
230
231#endif
232