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