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