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